How to send / Receive text message with Telegram bot

3,391 views
Skip to first unread message

Mauritz Zondagh

unread,
Apr 26, 2018, 11:40:47 AM4/26/18
to DroidScript
Hi, I followed Alex.Symbroson example for sending a simple text message via Telegram. I do not get it to work, can someone please help me.
I installed Telegram on my phone, requested a bot, named it, with username and received a token.
So this token is the address to whom i want to send my message (token = address) from my Droidscript app?

Here is my code (Alex sample txt code inserted into Droidscript app)

var token = "I removed my BOT token from here for the purpose of posting it in the public domain",
    addr = "https://api.telegram.org/bot"+token+"/",
    offset = 1,
    chatid = "" //chat id of the bot chat
var msg = {"chat_id":chatid, "text":"MY MESSAGE I WANT TO SEND"}
 
function OnStart()
{
  lay = app.CreateLayout( "linear", "VCenter,FillXY" );
  txt = app.CreateText( "Hello" );
  txt.SetTextSize( 32 );
  lay.AddChild( txt );
  app.AddLayout( lay );

  newHttpRequest("post","sendMessage",msg)

}

function newHttpRequest(type,func,asset)
{
  var httpRequest=new XMLHttpRequest();
  httpRequest.onreadystatechange=function()
  {
    if(httpRequest.readyState==4)
    {
      with(httpRequest)
      {
        alert(httpRequest.response)
        handle(JSON.parse(httpRequest.response).result)
      }
    }
  };
  httpRequest.open(type,addr+func,true);
  httpRequest.send(JSON.stringify(asset));
}

function handle(msgs)
{
  alert(JSON.stringify(msgs))
  if(msgs) msgs.forEach( function(msg)
  {
    if(msg.update_id>offset) offset=msg.update_id

  })
}

The alert message displays error : {"ok:false,"error_code":400,"description":"Bad Request: message text is empty"} followed by message box displaying undefined

Thanks in advance.

alex.symbroson

unread,
Apr 26, 2018, 12:42:19 PM4/26/18
to DroidScript
Hi Mauritz,

The problem is that the content type json header is missing - so you need to add 'httpRequest.setRequestHeader("Content-Type", "application/json");'

Note that I couldn't get sending files (files, images, stickers) working when I tried it in the past. But it would be nice if you'd tell me if you have found a solution for that :)
btw here's an updated version of the template:


var token = "<BOT_TOKEN>", / /your telegram bot token from the BotFather

    addr = "https://api.telegram.org/bot"+token+"/",
    offset = 1, // will be overridden by getUpdates
    chatid = "<BOT_CHAT_ID>"; // chat id of the bot chat

var msg = {"chat_id": chatid, "text": "My Text"};
 
function OnStart() {
    newHttpRequest("get", "getUpdates", null, handleUpdates);
    newHttpRequest("post", "sendMessage", msg, handleResult);
}

function newHttpRequest(type, func, asset, callback) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() {
        if(httpRequest.readyState == 4 && callback)
            callback(JSON.parse(httpRequest.response));
    };
   
    httpRequest.open(type, addr + func, true);
    httpRequest.setRequestHeader("Content-Type", "application/json");
    httpRequest.send(JSON.stringify(asset));
}

function handleUpdates(result) {
    if(result.ok) {
        result.result.forEach(function(msg) {
            alert("update:\n" + JSON.stringify(msg));
            if(msg.update_id >= offset)
                offset = msg.update_id + 1;
        });
    } else
        alert("Telegram error " + result.error_code + ":\n" + result.description);
}

function handleResult(result) {
    if(result.ok)
        alert("result:\n" + JSON.stringify(result.result));
    else
        alert("Telegram error " + result.error_code + ":\n" + result.description);
}

Mauritz Zondagh

unread,
Apr 26, 2018, 1:06:40 PM4/26/18
to DroidScript
Hi Alex,

I am only starting with this - trying to send a message. Probably sending a file would be a next step. If i get something going with a file, i will definitely  post a example on the group.

If i use your new template, what is the CHAT ID? Where do i get this?

    chatid = "<BOT_CHAT_ID>"; // chat id of the bot chat

I get a error "chat not found", if i make the chat ID 0 or 1. Just tried :)

Thanks


alex.symbroson

unread,
Apr 26, 2018, 1:09:04 PM4/26/18
to DroidScript
You can get it by posting something into your bot chat and then calling getUpdates - there you'll see your chat id

Mauritz Zondagh

unread,
Apr 26, 2018, 1:38:41 PM4/26/18
to DroidScript
OK, could not get the id with getUpdates. Not sure where to use it and how.
However, Google provided a answer. 
Type the following into your browser
Look at the results in browser and your will see messages, all with the same id e.g.
......"language_code":"en-za"},"chat":{"id":457567634,"first_name":.......
This id is your chat ID.

And Voila, a message popped up into Telegram!!

Thanks

alex.symbroson

unread,
Apr 26, 2018, 2:09:32 PM4/26/18
to DroidScript
You just had to call newHttpRequest("get", "getUpdates", null, handleUpdates); like in my example - normally the result should show every update and also the sender chat id

Mauritz Zondagh

unread,
Apr 26, 2018, 2:58:21 PM4/26/18
to DroidScript
Thanks, i have it working now
With your example, can send and receive, just need to do the parsing. :)

Alessio Arangia

unread,
Apr 5, 2022, 9:05:08 AM4/5/22
to DroidScript
Ciao, ho seguito il vostro esempio e ricevu msg su telegram, ma se io volessi mandare una richiesta su telegram di info e una risposta con dei valori di variabili? grazie in anticipo
Reply all
Reply to author
Forward
0 new messages