1

I followed this website on setting up a webhook between my Telegram Bot and Apps Script webapp. I setup the webhook according to the guide and deploy the webapp. Nothing happens when I send any message to the bot (the bot is supposed to echo the message). The app receives no POST request (at least according to the logger) when I do so, indicating a problem with the webhook.

For testing purposes, the webApp simply echoes whatever message the bot receives back to the sender.

I setup the webhook by visiting https://api.telegram.org/bot{API_TOKEN}/setWebHook?url={CURRENT_WEB_APP_URL} and get a "webhook is set" message in response.

I then deploy the webapp and send one "test" message to the bot. No response from the bot, and nothing logged in the built in logger. Sending a getWebhookInfo yields the following:

ok  true
result  
url "https://script.google.com/macros/s/XXXXXXXXXXXXXX/exec"
has_custom_certificate  false
pending_update_count    0
max_connections 40
function doPost(e)
{
  Logger.log(e);
  var contents = JSON.parse(e.postData.contents);
  var id = contents.message.from.id;
  var userID = contents.message.from.username;
  var text = contents.message.text.split(' ');
  var name = contents.message.from.first_name;
  sendMessage(id, text);


}

function sendMessage(id, text)
{
  var url = telegramAPI + "/sendMessage?chat_id="+id+"&text"+text;
  var response = UrlFetchApp.fetch(url);
}
function setWebhook()
{
  var url = telegramAPI + "/setWebhook?url="+webAppUrl;
  var response = UrlFetchApp.fetch(url);
  Logger.log(response);
}

I expect the bot to reply but nothing happens. Any clue on what's wrong?

  • In `var url = telegramAPI + "/sendMessage?chat_id="+id+"&text"+text;` what value have you set for `telegramAPI`? – ADW Aug 18 '19 at 15:51
  • ```telgramAPI = "https://api.telegram.org/bot"+ token; ``` – Ravyu Sivakumaran Aug 18 '19 at 16:05
  • So no bugs there. What type of chat are you sending the message to the bot in: `private`, `group`, `supergroup` or `channel`? – ADW Aug 18 '19 at 16:15
  • Its a private chat. So far I've been feeding single word messages to no avail – Ravyu Sivakumaran Aug 18 '19 at 16:18
  • Damn. I looked at the sendMessage function again. The text parameter was missing an "=" at the end. Sorry for wasting your time. Thank you very much – Ravyu Sivakumaran Aug 18 '19 at 16:23
  • Glad it got fixed. A quick tip that may help. You are capturing the `from.id` but sending the message to `chat.id`. This works in a private chat but not in the other types of chats. See [this](https://stackoverflow.com/questions/42785390/what-is-difference-between-msg-chat-id-and-msg-from-id-in-telegeram-bot/42786449). – ADW Aug 18 '19 at 16:27
  • That's a neat tip! I didn't think there'd be a difference but it makes sense. Tyvm! – Ravyu Sivakumaran Aug 18 '19 at 16:57

1 Answers1

0
var url = telegramAPI + "/sendMessage?chat_id="+id+"&text"+text;

Added "=" to "&text" and it worked. Sorry for wasting your time.