Telegram Bot - HTTP Client issue

Posted on
  • Hi guys,

    i try to send a messages from the ESP8266-01 by using the espruino firmware.

    >process.env
    ={
      VERSION: "2v03",
      GIT_COMMIT: "e77d74f6",
      BOARD: "ESP8266_BOARD",
      FLASH: 0, RAM: 81920,
      SERIAL: "807d3a69-78f3",
      CONSOLE: "Telnet",
      MODULES: "Flash,Storage,hea" ... "r,crypto,neopixel",
      EXPTR: 1073643636 }
    

    The wifi connection is established successfully.

        var request = http.get(encodeURIComponent("http://api.­telegram.org/bot{REPLACED_WITH_TOKEN}/se­ndMessage?chat_id={REPLACE_WITH_MY_ID}&t­ext=HI"), function(res) {
            res.on('data', function(data) {
                console.log("RESPONSE:", data);
            });
          res.on('close', function() { 
            console.log(res);
          });
        });
      console.log(request);
      request.on('error', (err) => {
      console.log(err);
    });
    

    Response of the error case:

    { "code": -6,
      "message": "not found"
     }
    

    After that I try to send a message via GET or POST to the telegram bot.
    Unfortunately I always get error messages. I am not sure if the requests arrive at telegram at all.
    Can someone give me a hint?

    I have already found a module in the internet. But that also do not work.
    http://wiki.amperka.ru/js:telegram

    Edit: If I just open the url in the browser the request will send and the telegram bot has the message.

  • Can you get a 200 response using a tool like Postman or Insomnia with the same endpoint? That's the first thing I'd check. That error suggests host is not found.

    Also, but without knowing, I'd also check whether http is supported by Telegram. I would expect an API like that to use https (in which case you have a problem on ESP8266).

  • Hi.

    Yes. I already made some requests with postman. http and https work in postman without any issues on the same endpoint.

    Why there is a problem with http on esp8266?

    Edit:

    >var request = http.get(
    :encodeURIComponent("http://api.thecatap­i.com/v1/images/search?size=full"),
    :(response) => {
    :console.log(response);
    :response.on('data', (data) => {console.log(data);})
    :});
    :request.on('error', (err) => {console.log(err)});
    =undefined
    { "code": -6,
      "message": "not found"
     }
    

    Now I'm sure that is isn't an issue with telegram bot.

    Edit2:

    var http = require("http");
    http.get("http://www.espruino.com", function(res) {
      res.on('data', function(data) {
        console.log(data);
      });
    });
    

    This works fine. I got the whole html page...

  • I think ESP8226 build does not support encodeURIComponent(). See the docs - not available on devices with low flash memory. Try it without.

  • >encodeURIComponent("https://www.google.­de")
    ="https%3A%2F%2Fwww.google.de"
    

    Seems to work fine.
    Code compiles without any problems.

    Latest result:

    >var options = url.parse("http://api.telegram.org/bot{M­YTOKEN}/getUpdates")
    ={
      protocol: "http:",
      method: "GET",
      host: "api.telegram.org",
      path: "/bot{MYTOKEN}" ... "{MYTOKEN}/getUpdates",
      pathname: "/bot{MYTOKEN}" ... "{MYTOKEN}/getUpdates",
      search: null, port: null, query: null }
    >var request = require("http").request(options, function(res) {
    :  res.on('data', function(data) {
    :    console.log("HTTP> "+data);
    :  });
    :  res.on('close', function(data) {
    :    console.log("Connection closed");
    :  });
    :});
    :request.on('error', (err) => {console.log(err);});
    =undefined
    >request.end()
    =undefined
    HTTP> <html>
    <head>
    HTTP> <title>301 Moved Permanently</title></head>
    <body bgcolor="white">
    <center><h1>301 Moved Permanently</h1></center>
    <hr><center>nginx/1.12.2</center>
    </body>
    </html>
    Connection closed
    

    The "301 Moved Permanently" should be a real result of telegram. Why it didn't work in espruino. No issues on browser or on postman.

  • Request with curl results in the same.

    No problem on espruino. Telegram didn't support http. Only https.

    Thank you - My fault :)

    Why espruino didn't support https on esp8266?

  • Ok, glad sorted. Re https, there's not enough memory on the device for the certificate management. You can use official Espruino Wifi board, and I think also supported on ESP32.

  • There's also this proxy which may work for you. http://httptohttps.mrtimcakes.com/ by espruino forum member @MrTimcakes

  • That page does not seem to work anymore.
    But it's no problem, I just run a http to https proxy as node application myself.

    Works great.
    @Ollie Thank you for your support :) Thumbs up :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Telegram Bot - HTTP Client issue

Posted by Avatar for user101402 @user101402

Actions