-1

I've been trying to build a simple telegram bot using google sheets script editor. I built it using this tutorial: https://phpvideotutor.com/telegram-bot-tutorial-how-to-connect-your-telegram-bot-to-a-google-spreadsheet-apps-script/

Now I'm trying to use the sendText function to send a text to the user with line breaks in it, but it seems to break the code everytime I try doing so using \n.

I've also tried wrapping my text in backticks instead of double quotes but it doesn't work.

I'm sure it's an easy enough fix, hopefully someone here could help!

Colin
  • 11
  • Can you share your code here? At the very least the sendText function and the doPost function. It could be a typo in your code so is hard for us to debug without seeing it. – James Payne Jan 21 '20 at 09:27
  • Hi, please try to provide a [minimal reproduce code](https://stackoverflow.com/help/minimal-reproducible-example). Also take a look at [How To Ask](https://stackoverflow.com/help/how-to-ask) to try to improve your question. Could you please try to debug your code so we can see how does the Message resource you are trying to send? – Raserhin Jan 21 '20 at 09:58
  • Hi all, so sorry that I didn't ask my question more clearly. Here's my github gist of it: https://gist.github.com/coolin02/439cfa617441983bac7b378d35460be1 For example, if I try to change line 45 from var answer = "Great work " + name + "! Keep it up! You are AMAZING"; to var answer = "Great work " + name + "! \nKeep it up! You are AMAZING"; Then the telegram bot doesn't reply at all, and my google sheets does not get updated with the scores too. I'm not sure how to debug this to find out where it's going wrong, and thought it was simply a syntax issue. – Colin Jan 23 '20 at 06:42

1 Answers1

1

I believe the reason may be that you are not encoding correctly your characters.

In the Telegram Bot API there are several ways to actually send your requests. From the documentation:

We support GET and POST HTTP methods. We support four ways of passing parameters in Bot API requests:

  • URL query string
  • application/x-www-form-urlencoded
  • application/json (except for uploading files)
  • multipart/form-data (use to upload files)

From your code you are sending a GET request using URL query strings, but you have not encoded your new line characters. In fact there are more questions about this in stack.

So basically you need to encode your special characters to appear in a compliant way (\n in this case happens to be %0A).


Alternatively to use \nwould be to make the request as a POST passing the message inside the payload, refer to the fetch documentation, and the sendMessage() from Telegram.

Raserhin
  • 1,999
  • 1
  • 5
  • 13