I want to use Google App Script to let a Telegram Bot post a message in a chat. That works fine, when I use this line of Code:
var message = UrlFetchApp.fetch(url + "/sendMessage?chat_id=" + chat_id_Testgruppe + "&text=HelloWorld");
But if I replace the “HelloWorld” with a variable like this:
var body = threads[i].getMessages().pop().getPlainBody(); // Gets the message-String from a Gmail-Thread
var message = UrlFetchApp.fetch(url + "/sendMessage?chat_id=" + chat_id_Testgruppe + "&text=" + body);
Then I get an invalid Argument error, even if the argument, which is shown me as an illegal argument for the fetch function, works, if I copy and paste it to my browser.
2
Answers
I needed to use encodeURIComponent() on my variable, that solved the problem.
The Telegram API reference specifies that getMessages() returns you a LIST of messages – that is an array. You need to access a single message (an array entry of the list) in order to obtain its content as a string.