Bamidbar22:28

וַיִּפְתַּח יְהוָה אֶת־פִּי הָאָתוֹן וַתֹּאמֶר לְבִלְעָם מֶה־עָשִׂיתִי לְךָ כִּי הִכִּיתַנִי זֶה שָׁלֹשׁ רְגָלִים׃

And the Eternal opened the mouth of the she-donkey, and she said to Bilam: "What have I done to you, that you have struck me these three times?"

Bamidbar 22:28

וַיִּפְתַּח יְהוָה אֶת־פִּי

Bamidbar / Numbers 22:28 · Pshat

Bamidbar 22:28

וַיִּפְתַּח יְהוָה אֶת־פִּי הָאָתוֹן וַתֹּאמֶר לְבִלְעָם מֶה־עָשִׂיתִי לְךָ כִּי הִכִּיתַנִי זֶה שָׁלֹשׁ רְגָלִים׃

And the Eternal opened the mouth of the she-donkey, and she said to Bilam: "What have I done to you, that you have struck me these three times?"

Pshat פְּשָׁט

The mouth of the she-donkey

Sometimes the truth is told to you by the one you least expect it from. And so it sounds louder. Bilam, proud of his gift of speech, hears a reproach from his own she-donkey.

Sforno: the Eternal gave the she-donkey the power to speak so that Bilam (Balaam) would remember that the gift of speech is given and taken by the Eternal, and would return in repentance. Kli Yakar: the she-donkey showed Bilam whom he resembles: prophecy is not natural to him either, and his mouth was opened only for a time, for the honour of Israel.

How Sforno and Kli Yakar explain it

Sforno And the Eternal opened the mouth of the she-donkey

He gave her the power to speak, as it says: "Lord, open my lips." And all this was so that Bilam (Balaam) would be stirred to return in repentance, remembering that "from the Eternal is the answer of the tongue" even for one who is not ready for it, and all the more can He take it from one who is ready, as He wills. And all this so that such a man as he would not be lost.

Kli Yakar And the Eternal opened the mouth of the she-donkey

It seems that this too was a need of the hour: to show him that he is like this donkey, whose nature is not to speak, and only for the honour of Israel the Eternal opened its mouth. So too Bilam: only for a time the Eternal opened his mouth with prophecy for the honour of Israel, and so that the nations would not say: "Had we had prophets, we would have mended our ways."

Takeaway

"Lord, open my lips" (Tehillim / Psalms 51:17). "But from the Eternal is the answer of the tongue" (Mishlei / Proverbs 16:1).
Sforno writes that everything was for Bilam's (Balaam's) sake: so that such a man would return and not be lost.
"Had we had prophets, we would have mended our ways": Kli Yakar explains why the Eternal gave prophecy to Bilam, a non-Jew.