In the Old Testament, after the Hebrews were freed from Egypt and given the Law at Sinai, they wandered the desert for forty years before reaching Israel. During that time, they were subjected to many hardships at God's will, testing them for worth. One test involved a man named Balaam, who was a noted prophet.
As the Hebrews had been attacked by and defeated two major kings, King Balak of Moab sent for Balaam to curse the Hebrews and cause their downfall. Balaam was a true prophet, although of low moral character, and he warned Balak that he was compelled to speak God's will regardless of persuasion. After a fable-type incident where Balaam's donkey speaks and admonishes him, Balaam reached Balak and performed sacrifices to Baal, a false god, to declare his curse. However, the true God spoke through him, blessing the Hebrews three times, and secured Balak's defeat.
Balaam is a complex character in the Old Testament, as he is not exclusively an evil man, but one who is subject to his own moral weakness. When Balak offers him honor and money, Balaam agrees to curse the Hebrews, who have not offended him, but he is aware that his prophetic gift comes from God, who is sympathetic to the Hebrews. Because his gift is true, Balaam's prophecies are positive rather than negative; his recognition of this and his varied repentance are the only thing that spares him from death.