"And God happened upon Balaam" (Numbers 23:4) — this is an expression of impurity, as you say, "a nocturnal occurrence" (Deuteronomy 23:11). That is to say, when He spoke with him, He regarded it as though He were speaking to one who is impure; and were it not for the love of Israel, He would not have spoken with him after he transgressed His word. "And he said to Him: I have arranged the seven altars" (Numbers 23:4) — Balaam said before the Holy One, blessed be He: Behold, Israel arranged for You but one altar, whereas I have arranged for You seven altars. Is it not better for You to leave Israel and to be with us? This is what is written: "Will the LORD be pleased with thousands of rams" (Micah 6:7).
Why God Spoke to Balaam Only for Israel's Sake
Midrash Aggadah, Numbers 23:4
ויקר אלהים אל בלעם. לשון טומאה, כמה דתימר מקרה לילה (דברים כג יא), כלומר כשהיה מדבר עמו היה חושב כאלו מדבר לטמא, ואלמלא בשביל אהבתן של ישראל, לא היה מדבר עמו, אחר שעבר על דיבורו: ויאמר אליו את שבעה המזבחות ערכתי. אמר בלעם לפני הקב"ה והלא ישראל לא ערכו לך אלא מזבח אחד, ואני ערכתי לך שבעה מזבחות, והלא טוב לך שתניח את ישראל ותהיה עמנו, הדא הוא דכתיב הירצה ה' באלפי אלים וגו' (מיכה ו ז):