Tasmota: Telegram message notification support

Created on 5 Jun 2020  路  74Comments  路  Source: arendst/Tasmota

Have you looked for this feature in other issues and in the docs?
Thank you for this great software. Yes, I searched in the repsitory and can not see related result.
Is your feature request related to a problem? Please describe.
_A clear and concise description of what the problem is._
No, it is not a problem
Describe the solution you'd like
_A clear and concise description of what you want to happen._
I want to send a notification as Telegram message so that I can recieve to my phone anywhere.
Describe alternatives you've considered
_A clear and concise description of any alternative solutions or features you've considered._
There are IOT MQTT alternatives for notification but they are not straightforward.
Additional context
_Add any other context or screenshots about the feature request here._
I noticed following arduino library in the github and maybe it is easy to integrate it to Tasmota
https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot

(Please, remember to close the issue when the problem has been addressed)

Add to Docs enhancement fixed

Most helpful comment

Initial support for Telegram messaging allows for sending commands to a Tasmota device and receiving response. It also allows sending (user)text from Tasmota to Telegram.

As comms between the two is slow (every message needs at least 1 second to complete connection and transfer) it's more like a gadget than a feature.

Anyway. Enabling Telegram support needs at least uncomment of define USE_TELEGRAM and uncomment of either define USE_TELEGRAM_FINGERPRINT OR define USE_MQTT_TLS_CA_CERT

Default, once enabled in user_config_override it does nothing. It needs to know your bots token and chat id. Enter them with command TmToken and TmChatId.

There are three state commands allowing control over the comms.

  • First to test your connection try TmState 5 allowing for echoing of your bot chat text.
  • Second stop the echo with TmState 4 and enable sending text from Tasmota to telegram with command TmState 1. Now enter on tasmota TmSend Howdy which should report Howdy in telegram.
  • Now in addition to sending from Tasmota you may enable receiving commands from telegram with option TmState 3. Enter any tasmota command in telegram and watch it's response in both tasmota AND in the chat window.

Default polling time is 10 seconds but can be changed with command TmPoll from 4 to 300 seconds.

Complete command syntax can be found in the header of file xdrv_40_telegram.ino

EDIT: Command TmState options are NOT persistent so if needed use rules to keep them in a state you like.

All 74 comments

That would be Awesome!
I can imagine a command like notify "..." for rules or sensor values!!

Been working on this yesterday but failed to get a https connection to api.telegram.org

All mentionend libraries fail in this case as is also reported by their different issues.

Tried a very basic approach using all SSL provided libraries but still fail to make a connection.

If anyone manages to make a https connection using the current core version I'll be interessted to dig in it again.

Theo, I know very well the SSL part. I can try.

This is the driver code currently not using any library other than the available BearSSL.

xdrv_50_telegram.zip

You'll need this too

tasmota.zip

And define USE_TELEGRAM in user_config_overrid.h

I am excited to see the result. Even we can send special format messages to Tasmota to enable or disable functions or rules over internet without the need for an IOT account. Also we can collect some sensor values periodically with another Telegram bot and setup our own IOT dashboard for free.

@ademirci well a plain XMPP/Jabber client would be enougth to reach public or private XMPP/Jabber chat server. And, furthermore, the desired function could be performed by the MQTT servers instead of overloading the client.
@arendst I would rather invest the time in support for an open federal platform than support a proprietary solution like Telegram.

Telegram api is HTTP not XMPP which makes it far easier to integrate in Tasmota.

What do you mean by "open federal platform"?

@s-hadinger wrote:

What do you mean by "open federal platform"?

Anyone can run a server and you do not need to have an account on the same server where the chat partner has one. Classic examples are Email and XMPP/Jabber, but also Mastodon.

At first glance, api.telegram.org uses a certificate signed by GoDaddy Daddy Secure Certificate Authority - G2. The server key is 2048 bits and the proposed cipher is ECDHE-RSA-AES128-GCM-SHA256 which is exactly the cipher used with AWS IoT.

This is all good news, apart from adding the CA in Flash, it should run smoothly. Stay tuned.

If you're curious about this, try openssl s_client -showcerts -connect api.telegram.org:443

Just did. Nice find!

And here is the CA ready to be munched by BearSSL (added at the end of tasmota_ca.ino:

#ifdef USE_TELEGRAM
// we add a separate CA for telegram

/*********************************************************************************************\
 * GoDaddy Daddy Secure Certificate Authority - G2, RSA 2048 bits SHA 256, valid until 20220523
 *
 * to convert do: "brssl ta GoDaddyCA.pem"
 * then copy and paste below, chain the generic names to the same as below
 * remove "static" and add "PROGMEM"
\*********************************************************************************************/

const unsigned char GoDaddyCAG2_DN[] PROGMEM = {
    0x30, 0x3E, 0x31, 0x21, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13,
    0x18, 0x44, 0x6F, 0x6D, 0x61, 0x69, 0x6E, 0x20, 0x43, 0x6F, 0x6E, 0x74,
    0x72, 0x6F, 0x6C, 0x20, 0x56, 0x61, 0x6C, 0x69, 0x64, 0x61, 0x74, 0x65,
    0x64, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x10,
    0x61, 0x70, 0x69, 0x2E, 0x74, 0x65, 0x6C, 0x65, 0x67, 0x72, 0x61, 0x6D,
    0x2E, 0x6F, 0x72, 0x67
};

const unsigned char GoDaddyCAG2_RSA_N[] PROGMEM = {
    0xB4, 0xA3, 0x16, 0x9E, 0x5C, 0x57, 0xC9, 0x89, 0x65, 0xED, 0xEA, 0x78,
    0x0B, 0xAE, 0x8A, 0x58, 0x2F, 0xAE, 0x5A, 0xC8, 0x6E, 0x49, 0x8D, 0xFC,
    0x57, 0xA5, 0x98, 0x88, 0x78, 0x2E, 0x0B, 0x3C, 0x40, 0x3C, 0x21, 0x2E,
    0x9A, 0x94, 0x98, 0x33, 0xA7, 0xE3, 0x42, 0xA7, 0x85, 0xFA, 0xD0, 0x73,
    0x84, 0x01, 0x1C, 0x72, 0x39, 0x37, 0x23, 0xB5, 0x56, 0x1D, 0x43, 0xA5,
    0x71, 0x14, 0x08, 0x24, 0xA5, 0x39, 0xCC, 0xDE, 0x58, 0x53, 0x94, 0x8E,
    0x2A, 0x42, 0xA7, 0x4E, 0x2D, 0x07, 0x32, 0x9E, 0xBA, 0x8B, 0xD3, 0x2A,
    0xA9, 0x9E, 0xC0, 0xE3, 0xCE, 0x9A, 0x10, 0x96, 0x45, 0x58, 0x7A, 0xC7,
    0x1E, 0x45, 0x14, 0x23, 0x92, 0xBB, 0x54, 0x82, 0x88, 0x94, 0x49, 0xB6,
    0xBE, 0x81, 0x21, 0x00, 0x29, 0x6D, 0xC9, 0xCE, 0x8B, 0x39, 0x3A, 0xDC,
    0x35, 0x15, 0xD9, 0xEB, 0x47, 0x9C, 0xEF, 0xBA, 0x09, 0x0E, 0x16, 0xE4,
    0xD9, 0xEB, 0x72, 0x30, 0xFA, 0x49, 0xAB, 0x98, 0x31, 0x7C, 0xB3, 0xAC,
    0x2B, 0x29, 0x91, 0x87, 0x08, 0x41, 0x72, 0x5E, 0x35, 0xC7, 0x87, 0x04,
    0x22, 0xF5, 0x48, 0x76, 0x30, 0x6D, 0x88, 0xDF, 0xF2, 0xA5, 0x29, 0x13,
    0x70, 0xB3, 0x87, 0x02, 0xD5, 0x6B, 0x58, 0xB1, 0xE8, 0x73, 0xC7, 0xE4,
    0xEF, 0x79, 0x86, 0xA4, 0x07, 0x5F, 0x67, 0xB4, 0x79, 0x8D, 0xA4, 0x25,
    0x01, 0x82, 0x8C, 0xE0, 0x30, 0x17, 0xCB, 0x4B, 0x5C, 0xFB, 0xEB, 0x4C,
    0x12, 0x51, 0xB9, 0xC9, 0x04, 0x1F, 0x7E, 0xD2, 0xF8, 0xBA, 0xF5, 0x35,
    0x8D, 0x8A, 0x1C, 0x37, 0x82, 0xF0, 0x15, 0x73, 0x00, 0x6E, 0x3D, 0x1C,
    0x76, 0x8B, 0x01, 0x74, 0x81, 0x3D, 0xE4, 0x2C, 0xA7, 0xCC, 0x2F, 0x66,
    0xDC, 0x44, 0xA8, 0x27, 0x3F, 0xEA, 0xD0, 0xA7, 0xA8, 0xF1, 0xCB, 0xEA,
    0xDA, 0x07, 0x38, 0xBD
};

const unsigned char GoDaddyCAG2_RSA_E[] PROGMEM = {
    0x01, 0x00, 0x01
};

const br_x509_trust_anchor GoDaddyCAG2_TA[1] PROGMEM = {
    {
        { (unsigned char *)TA0_DN, sizeof TA0_DN },
        0,
        {
            BR_KEYTYPE_RSA,
            { .rsa = {
                (unsigned char *)GoDaddyCAG2_RSA_N, sizeof GoDaddyCAG2_RSA_N,
                (unsigned char *)GoDaddyCAG2_RSA_E, sizeof GoDaddyCAG2_RSA_E,
            } }
        }
    }
};

#endif // USE_TELEGRAM

The downside is that the certificate is valid until May 2022, so we will need to update regularly.

I will not be available this afternoon to test the whole, but here is how it should work:

#include "WiFiClientSecureLightBearSSL.h"
BearSSL::WiFiClientSecure_light *tlsClient;

[...]
  tlsClient = new BearSSL::WiFiClientSecure_light(1024,1024);
  tlsClient->setTrustAnchor(&GoDaddyCAG2_TA);

Then pass tlsClient to the Telegram lib.

Edit: it would be interesting to print tlsClient->getMFLNStatus() after the connection to see if we risk dropping messages of more than 1024 bytes.

If connection failed, tlsClient->getLastError() will give the SSL error number.

Thx I'll give it a try today

Oh I forgot, you need to set #define USE_MQTT_TLS_FORCE_EC_CIPHER to force ECDHE-RSA-AES128-GCM-SHA256

I'm trying now, actually here is the CA:

#ifdef USE_TELEGRAM
// we add a separate CA for telegram

/*********************************************************************************************\
 * GoDaddy Daddy Secure Certificate Authority - G2, RSA 2048 bits SHA 256, valid until 20220523
 *
 * to convert do: "brssl ta GoDaddyCA.pem"
 * then copy and paste below, chain the generic names to the same as below
 * remove "static" and add "PROGMEM"
\*********************************************************************************************/

const unsigned char GoDaddyCAG2_DN[] PROGMEM = {
    0x30, 0x3E, 0x31, 0x21, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x13,
    0x18, 0x44, 0x6F, 0x6D, 0x61, 0x69, 0x6E, 0x20, 0x43, 0x6F, 0x6E, 0x74,
    0x72, 0x6F, 0x6C, 0x20, 0x56, 0x61, 0x6C, 0x69, 0x64, 0x61, 0x74, 0x65,
    0x64, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x10,
    0x61, 0x70, 0x69, 0x2E, 0x74, 0x65, 0x6C, 0x65, 0x67, 0x72, 0x61, 0x6D,
    0x2E, 0x6F, 0x72, 0x67
};

const unsigned char GoDaddyCAG2_RSA_N[] PROGMEM = {
    0xB4, 0xA3, 0x16, 0x9E, 0x5C, 0x57, 0xC9, 0x89, 0x65, 0xED, 0xEA, 0x78,
    0x0B, 0xAE, 0x8A, 0x58, 0x2F, 0xAE, 0x5A, 0xC8, 0x6E, 0x49, 0x8D, 0xFC,
    0x57, 0xA5, 0x98, 0x88, 0x78, 0x2E, 0x0B, 0x3C, 0x40, 0x3C, 0x21, 0x2E,
    0x9A, 0x94, 0x98, 0x33, 0xA7, 0xE3, 0x42, 0xA7, 0x85, 0xFA, 0xD0, 0x73,
    0x84, 0x01, 0x1C, 0x72, 0x39, 0x37, 0x23, 0xB5, 0x56, 0x1D, 0x43, 0xA5,
    0x71, 0x14, 0x08, 0x24, 0xA5, 0x39, 0xCC, 0xDE, 0x58, 0x53, 0x94, 0x8E,
    0x2A, 0x42, 0xA7, 0x4E, 0x2D, 0x07, 0x32, 0x9E, 0xBA, 0x8B, 0xD3, 0x2A,
    0xA9, 0x9E, 0xC0, 0xE3, 0xCE, 0x9A, 0x10, 0x96, 0x45, 0x58, 0x7A, 0xC7,
    0x1E, 0x45, 0x14, 0x23, 0x92, 0xBB, 0x54, 0x82, 0x88, 0x94, 0x49, 0xB6,
    0xBE, 0x81, 0x21, 0x00, 0x29, 0x6D, 0xC9, 0xCE, 0x8B, 0x39, 0x3A, 0xDC,
    0x35, 0x15, 0xD9, 0xEB, 0x47, 0x9C, 0xEF, 0xBA, 0x09, 0x0E, 0x16, 0xE4,
    0xD9, 0xEB, 0x72, 0x30, 0xFA, 0x49, 0xAB, 0x98, 0x31, 0x7C, 0xB3, 0xAC,
    0x2B, 0x29, 0x91, 0x87, 0x08, 0x41, 0x72, 0x5E, 0x35, 0xC7, 0x87, 0x04,
    0x22, 0xF5, 0x48, 0x76, 0x30, 0x6D, 0x88, 0xDF, 0xF2, 0xA5, 0x29, 0x13,
    0x70, 0xB3, 0x87, 0x02, 0xD5, 0x6B, 0x58, 0xB1, 0xE8, 0x73, 0xC7, 0xE4,
    0xEF, 0x79, 0x86, 0xA4, 0x07, 0x5F, 0x67, 0xB4, 0x79, 0x8D, 0xA4, 0x25,
    0x01, 0x82, 0x8C, 0xE0, 0x30, 0x17, 0xCB, 0x4B, 0x5C, 0xFB, 0xEB, 0x4C,
    0x12, 0x51, 0xB9, 0xC9, 0x04, 0x1F, 0x7E, 0xD2, 0xF8, 0xBA, 0xF5, 0x35,
    0x8D, 0x8A, 0x1C, 0x37, 0x82, 0xF0, 0x15, 0x73, 0x00, 0x6E, 0x3D, 0x1C,
    0x76, 0x8B, 0x01, 0x74, 0x81, 0x3D, 0xE4, 0x2C, 0xA7, 0xCC, 0x2F, 0x66,
    0xDC, 0x44, 0xA8, 0x27, 0x3F, 0xEA, 0xD0, 0xA7, 0xA8, 0xF1, 0xCB, 0xEA,
    0xDA, 0x07, 0x38, 0xBD
};

const unsigned char GoDaddyCAG2_RSA_E[] PROGMEM = {
    0x01, 0x00, 0x01
};

const br_x509_trust_anchor GoDaddyCAG2_TA PROGMEM = {
    { (unsigned char *)GoDaddyCAG2_DN, sizeof GoDaddyCAG2_DN },
    0,
    {
        BR_KEYTYPE_RSA,
        { .rsa = {
            (unsigned char *)GoDaddyCAG2_RSA_N, sizeof GoDaddyCAG2_RSA_N,
            (unsigned char *)GoDaddyCAG2_RSA_E, sizeof GoDaddyCAG2_RSA_E,
        } }
    }
};

#endif // USE_TELEGRAM

I'm progressing. Call is done, I'm now getting error 296 which is SSL3_ALERT_HANDSHAKE_FAILURE

I will continue tomorrow. The current code is here: https://github.com/s-hadinger/Tasmota/tree/telegram_dev

I got a first version working by disabling CA validation, otherwise it seems to hang.

First good news is TLS was indeed connected, second good news is that Telegram supports MFLN so we can keep 1KB buffers. Bad news is it crashed in the destructor of HTTPClient, but I know why.

Fun fact: after doing lots of tests by IP address got black-listed. I needed to switch to mobile.

GET Update Messages
Client initialized
connect(api.telegram.org,443)
connect(149.154.167.220,443)
_connectSSL: start connection
Connected! MFLNStatus = 1
[HTTPS] GET... code: 404

Exception (28):
epc1=0x4023a0e7 epc2=0x00000000 epc3=0x40248b9d excvaddr=0x000002c0 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffc40 end: 3fffffc0 offset: 0190
3ffffdd0:  00000001 3fff2fb8 3fff4b84 40208e70

Crash fixed. It seems to connect correctly, since I get a 404 error because of a fake bot token.

Theo, the code shouldn't retry automatically when there is an error, because it ends up hammering the api endpoint and makes the ip address black-listed.

Log is very verbose for now. You need for now to #define USE_TELEGRAM and do not define any TLS option, especially don't enable USE_MQTT_TLS_CA_CERT (for now)

Progressing. Connection works well and I need to clean up code.

However the current logic reconnects every second, which is problematic: the actual SSL connection takes around 1 second and freezes the entire Tasmota. Is long polling possible? Or should me relax the frequency?

Also send does not seem to work (yet).

connect(api.telegram.org,443)
connect(149.154.167.220,443)
_connectSSL: start connection
Connected! MFLNStatus = 1
[HTTPS] GET... code: 200
[HTTPS] Received payload telegram:
1BTC = {"ok":true,"result":[]}USD
Sent Update request messages up to : 0
no new messages

Hi have a dilemma, for TLS server validation:

  1. validate the CA. Problem: the server certificate is bigger than 1024 bytes, so I need to increase the receive buffer from 1024 to 1536 bytes and eats up 512 more byte or RAM. Also, the certificate is only valid until 2022.
  2. is fingerprint validation of server public key. I have no clue about the stability of their public key, it may break tomorrow or stay stable for 10 years.
  3. don't check the server identity which is subject to man-in-the-middle attack.

I propose to implement both 1 and 2, it will depend on #define USE_MQTT_TLS_CA_CERT value.

Done. The TLS is fully working, both with CA validation or Fingerprint validation. If the fingerprint is wrong, the new one is shown in logs.

@arendst You can now work on the fun part :)

All needed changes are in this branch: https://github.com/s-hadinger/Tasmota/tree/telegram_dev

Thx for the basics. Will see tomorrow.

Just had a very quick look but failed to get anything running. Both my and your repository end in this:

00:00:03 WIF: Attempting connection...
00:00:04 WIF: Network 0, AP2, SSId indebuurt_IoT, Channel 1, BSSId 74:83:C2:2A:F1:AC, RSSI -85, Encryption 1
00:00:04 WIF: Network 1, AP2, SSId indebuurt_IoT, Channel 11, BSSId 18:E8:29:CA:17:C1, RSSI -23, Encryption 1
00:00:04 WIF: Connecting to AP2 indebuurt_IoT Channel 11 BSSId 18:E8:29:CA:17:C1 in mode 11N as wemos4...
00:00:04 WIF: Attempting connection...
00:00:05 WIF: Connected
00:00:05 HTP: Web server active on wemos4 with IP address 192.168.2.223
18:25:27 NTP: UTC 2020-06-08T16:25:26, DST 2020-03-29T02:00:00, STD 2020-10-25T03:00:00
18:25:27 TLG: Started
18:25:28 MQT: Attempting connection...
18:25:28 MQT: Connected
18:25:28 MQT: tele/wemos4/LWT = Online (retained)
18:25:28 MQT: cmnd/wemos4/POWER = 
18:25:28 MQT: Subscribe to cmnd/wemos4/#
18:25:28 MQT: Subscribe to cmnd/tasmotas/#
18:25:28 MQT: Subscribe to cmnd/DVES_83BB10_fb/#
18:25:28 MQT: tele/wemos4/INFO1 = {"Module":"Sonoff Basic","Version":"8.3.1.2(tasmota)","FallbackTopic":"cmnd/DVES_83BB10_fb/","GroupTopic":"cmnd/tasmotas/"}
18:25:28 MQT: tele/wemos4/INFO2 = {"WebServerMode":"Admin","Hostname":"wemos4","IPAddress":"192.168.2.223"}
18:25:28 MQT: tele/wemos4/INFO3 = {"RestartReason":{"Exception":4,"Reason":"Software Watchdog","EPC":["40003b53","00000000","00000000"],"EXCVADDR":"00000000","DEPC":"00000000"}}
18:25:28 MQT: stat/wemos4/RESULT = {"POWER":"OFF"}
18:25:28 MQT: stat/wemos4/POWER = OFF
GET Update Messages 
18:25:28 TLG: Cmnd bot1179906608:AAE-3UfPJt3u8d-gbGXslnFO96o3DahG6EM/getUpdates?offset=0

User exception (panic/abort/assert)
Abort called

>>>stack>>>

ctx: sys
sp: 3fff7ee0 end: 3fffffb0 offset: 0000
3fff7ee0:  f489b847 deadbeef 00000004 3fff6630  
3fff7ef0:  000000fe 00000000 00000000 00000000  
3fff7f00:  00000000 00000000 00000000 3fff7f30  
3fff7f10:  00000004 00000090 00000004 f489b847  
3fff7f20:  00000008 00000000 3fff5e4c 4024f63a  
3fff7f30:  3ffe892a 00000000 00000000 4024f64c  
3fff7f40:  3fff7c60 00000000 3fff5e4c 402085fd  
3fff7f50:  deadbeef 00000000 3fff5e4c 4020868c  
3fff7f60:  deadbeef 00000000 068508d8 40283e8c  
3fff7f70:  00000001 3fff54b8 40284128 40284128  
3fff7f80:  00000000 00000001 3fff81dc 00000100  
3fff7f90:  3fff82dc 00000003 3fff7fa4 3fff803c  
3fff7fa0:  4028d528 ffffffff 00000000 00000000  
3fff7fb0:  00000000 000000c2 00000000 0000001f  
3fff7fc0:  0000001f 000000ae 00000000 0000001f 

No worries. Will have a better look tomorrow.

You can try in WiFiClientSecureLightBearSSL.cpp:

#define DEBUG_TLS
#define DEBUG_ESP_SSL

Also try with and without #define USE_MQTT_TLS_CA_CERT

Frustrating but all I get is this:

00:00:01 HTP: Web server active on wemos4 with IP address 192.168.2.223
15:13:26 NTP: UTC 2020-06-09T13:13:25, DST 2020-03-29T02:00:00, STD 2020-10-25T03:00:00
15:13:26 TLG: Started
15:13:27 MQT: Attempting connection...
15:13:27 MQT: Connected
15:13:27 MQT: tele/wemos4/LWT = Online (retained)
15:13:27 MQT: cmnd/wemos4/POWER = 
15:13:27 MQT: Subscribe to cmnd/wemos4/#
15:13:27 MQT: Subscribe to cmnd/tasmotas/#
15:13:27 MQT: Subscribe to cmnd/DVES_83BB10_fb/#
15:13:27 MQT: tele/wemos4/INFO1 = {"Module":"Sonoff Basic","Version":"8.3.1.2(theo)","FallbackTopic":"cmnd/DVES_83BB10_fb/","GroupTopic":"cmnd/tasmotas/"}
15:13:27 MQT: tele/wemos4/INFO2 = {"WebServerMode":"Admin","Hostname":"wemos4","IPAddress":"192.168.2.223"}
15:13:27 MQT: tele/wemos4/INFO3 = {"RestartReason":"External System"}
15:13:27 MQT: stat/wemos4/RESULT = {"POWER":"OFF"}
15:13:27 MQT: stat/wemos4/POWER = OFF
15:13:31 MQT: tele/wemos4/STATE = {"Time":"2020-06-09T15:13:31","Uptime":"0T00:00:09","UptimeSec":9,"Heap":27,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":1,"POWER":"OFF","Wifi":{"AP":2,"SSId":"indebuurt_IoT","BSSId":"18:E8:29:CA:17:C1","Channel":11,"RSSI":100,"Signal":-28,"LinkCount":1,"Downtime":"0T00:00:03"}}
15:13:32 APP: Boot Count 161
15:13:32 CFG: Saved to flash at F5, Count 290, Bytes 4096
GET Update Messages 
StackThunk before 27496, Fragmentation=2, Thunkstack=0, Free stack=3664, FreeContStack=1680
StackThunk after 27496, Fragmentation=2, Thunkstack=0, Free stack=3664, FreeContStack=1680
allocateBuffers before 27496, Fragmentation=2, Thunkstack=0, Free stack=3616, FreeContStack=1680
allocateBuffers ClientContext 24056, Fragmentation=2, Thunkstack=0, Free stack=3616, FreeContStack=1680
allocateBuffers after 21528, Fragmentation=2, Thunkstack=0, Free stack=3616, FreeContStack=1680
15:13:33 TLG: Cmnd bot1179906608:AAE-3UfPJt3u8d-gbGXslnFO96o3DahG6EM/getUpdates?offset=0
connect(api.telegram.org,443)
connect(149.154.167.220,443)
Before calling _connectSSL 21192, Fragmentation=1, Thunkstack=0, Free stack=3632, FreeContStack=1680
_connectSSL.start 21192, Fragmentation=1, Thunkstack=0, Free stack=3600, FreeContStack=1680
Thunk allocated 17584, Fragmentation=1, Thunkstack=0, Free stack=3600, FreeContStack=1680
_connectSSL: start connection
_connectSSL before DecoderContext allocation 17584, Fragmentation=1, Thunkstack=0, Free stack=3600, FreeContStack=1680
_connectSSL after DecoderContext allocation 16432, Fragmentation=1, Thunkstack=0, Free stack=3600, FreeContStack=1680
_connectSSL before PrivKey allocation 16432, Fragmentation=1, Thunkstack=0, Free stack=3600, FreeContStack=1680
_wait_for_handshake: start

User exception (panic/abort/assert)
Abort called

>>>stack>>>

ctx: sys
sp: 3fff7b38 end: 3fffffb0 offset: 0000
3fff7b38:  f2532668 deadbeef 00000004 3fff5e18  
3fff7b48:  000000fe 00000000 00000000 00000000  
3fff7b58:  00000000 00000000 00000000 3fff7b88 

It seems it stalls in _run_until called from _wait_for_handshake but I don't see why.

It seems to be a conditional compile issue. Get the latest commit to enable CA validation without a private key, and compile with both #define USE_MQTT_TLS_CA_CERT and #define USE_MQTT_TLS_FORCE_EC_CIPHER.

I'll check later how to make sure it compiles with the right options.

I found the issue, the Stack Thunk was not correctly adjusted. Now the branch is good: https://github.com/s-hadinger/Tasmota/tree/telegram_dev

Continue testing ....

Progress using the attached xdrv_50_telegram.ino file

xdrv_50_telegram.zip

Still fails after three rounds but we will get there eventually...

18:16:54 CMD: tgstate 1
18:16:54 SRC: Serial
18:16:54 CMD: Group 0, Index 1, Command "TGSTATE", Data "1"
18:16:54 MQT: stat/wemos4/RESULT = {"TGState":{"Receive":"ON","Send":"OFF"}}
18:16:55 TLG: Started
18:16:56 TLG: Get update messages
StackThunk before 27392, Fragmentation=5, Thunkstack=0, Free stack=3648, FreeContStack=1788
StackThunk after 27392, Fragmentation=5, Thunkstack=0, Free stack=3648, FreeContStack=1788
allocateBuffers before 27392, Fragmentation=5, Thunkstack=0, Free stack=3600, FreeContStack=1788
allocateBuffers ClientContext 23952, Fragmentation=6, Thunkstack=0, Free stack=3600, FreeContStack=1788
allocateBuffers after 21424, Fragmentation=6, Thunkstack=0, Free stack=3600, FreeContStack=1788
18:16:56 TLG: Cmnd bot1179906608:AAE-3UfPJt3u8d-gbGXslnFO96o3DahG6EM/getUpdates?offset=0
connect(api.telegram.org,443)
connect(149.154.167.220,443)
Before calling _connectSSL 20440, Fragmentation=4, Thunkstack=0, Free stack=3616, FreeContStack=1788
_connectSSL.start 20440, Fragmentation=4, Thunkstack=0, Free stack=3584, FreeContStack=1788
Thunk allocated 15136, Fragmentation=5, Thunkstack=0, Free stack=3584, FreeContStack=1788
_connectSSL: start connection
_connectSSL before DecoderContext allocation 15136, Fragmentation=5, Thunkstack=0, Free stack=3584, FreeContStack=1788
_connectSSL after DecoderContext allocation 13984, Fragmentation=5, Thunkstack=0, Free stack=3584, FreeContStack=1788
_connectSSL before PrivKey allocation 13984, Fragmentation=5, Thunkstack=0, Free stack=3584, FreeContStack=1788
Connected! MFLNStatus = 1
_connectSSL.end 14096, Fragmentation=6, Thunkstack=4516, Free stack=3584, FreeContStack=1472
_connectSSL.end, freeing StackThunk 19400, Fragmentation=27, Thunkstack=0, Free stack=3584, FreeContStack=1472
_connectSSL after release of Priv Key 20552, Fragmentation=4, Thunkstack=0, Free stack=3584, FreeContStack=1472
18:16:57 TLG: Connected in 997 ms, max ThunkStack used 4516
_write 20464, Fragmentation=4, Thunkstack=0, Free stack=3568, FreeContStack=1472
_write 20464, Fragmentation=4, Thunkstack=0, Free stack=3552, FreeContStack=1472
18:16:57 TLG: Sent Update request messages up to 0
18:16:57 TLG: Msg 1 {"update_id":973125390,
"message":{"message_id":16,"from":{"id":139920293,"is_bot":false,"first_name":"Theo","last_name":"Arends","username":"arendst","language_code":"nl"},"chat":{"id":139920293,"first_name":"Theo","last_name":"Arends","username":"arendst","type":"private"},"date":1591805777,"text":"Tasmota is great"}}]}
18:16:57 TLG: Parsed0 "973125390"
18:16:57 TLG: Parsed1 "139920293"
18:16:57 TLG: Parsed2 "Theo"
18:16:57 TLG: Parsed3 "Arends"
18:16:57 TLG: Parsed4 "139920293"
18:16:57 TLG: Parsed5 "Tasmota is great"
18:16:57 TLG: Send message
StackThunk before 20488, Fragmentation=6, Thunkstack=0, Free stack=3696, FreeContStack=1472
StackThunk after 20488, Fragmentation=6, Thunkstack=0, Free stack=3696, FreeContStack=1472
allocateBuffers before 20488, Fragmentation=6, Thunkstack=0, Free stack=3648, FreeContStack=1472
allocateBuffers ClientContext 17048, Fragmentation=6, Thunkstack=0, Free stack=3648, FreeContStack=1472
allocateBuffers after 14520, Fragmentation=7, Thunkstack=0, Free stack=3648, FreeContStack=1472
18:16:57 TLG: Cmnd bot1179988888:AAE-3UfPJt3u8d-gbGXslnFAAAAAAahG6EM/sendMessage?chat_id=139920293&text=Tasmota is great&reply_markup=
connect(api.telegram.org,443)
connect(149.154.167.220,443)
Before calling _connectSSL 14184, Fragmentation=5, Thunkstack=0, Free stack=3664, FreeContStack=1472
_connectSSL.start 14184, Fragmentation=5, Thunkstack=0, Free stack=3632, FreeContStack=1472
Thunk allocated 8880, Fragmentation=8, Thunkstack=0, Free stack=3632, FreeContStack=1472
_connectSSL: start connection
_connectSSL before DecoderContext allocation 8880, Fragmentation=8, Thunkstack=0, Free stack=3632, FreeContStack=1472
_connectSSL after DecoderContext allocation 7728, Fragmentation=9, Thunkstack=0, Free stack=3632, FreeContStack=1472
_connectSSL before PrivKey allocation 7728, Fragmentation=9, Thunkstack=0, Free stack=3632, FreeContStack=1472
Connected! MFLNStatus = 1
_connectSSL.end 7840, Fragmentation=10, Thunkstack=4516, Free stack=3632, FreeContStack=1472
_connectSSL.end, freeing StackThunk 13144, Fragmentation=33, Thunkstack=0, Free stack=3632, FreeContStack=1472
_connectSSL after release of Priv Key 14296, Fragmentation=6, Thunkstack=0, Free stack=3632, FreeContStack=1472
18:16:58 TLG: Connected in 971 ms, max ThunkStack used 4516
_write 14160, Fragmentation=5, Thunkstack=0, Free stack=3616, FreeContStack=1472
_write 14160, Fragmentation=5, Thunkstack=0, Free stack=3600, FreeContStack=1472
18:16:58 TLG: Message sent
18:17:10 TLG: Get update messages
StackThunk before 14280, Fragmentation=6, Thunkstack=0, Free stack=3648, FreeContStack=1472
StackThunk after 14280, Fragmentation=6, Thunkstack=0, Free stack=3648, FreeContStack=1472
allocateBuffers before 14280, Fragmentation=6, Thunkstack=0, Free stack=3600, FreeContStack=1472
allocateBuffers ClientContext 10840, Fragmentation=7, Thunkstack=0, Free stack=3600, FreeContStack=1472
allocateBuffers after 8312, Fragmentation=8, Thunkstack=0, Free stack=3600, FreeContStack=1472
18:17:10 TLG: Cmnd bot1179988888:AAE-3UfPJt3u8d-gbGXslnFAAAAAAahG6EM/getUpdates?offset=973125391
connect(api.telegram.org,443)
connect(149.154.167.220,443)
Before calling _connectSSL 7976, Fragmentation=5, Thunkstack=0, Free stack=3616, FreeContStack=1472
_connectSSL.start 7976, Fragmentation=5, Thunkstack=0, Free stack=3584, FreeContStack=1472
Thunk allocated 2672, Fragmentation=13, Thunkstack=0, Free stack=3584, FreeContStack=1472
_connectSSL: start connection
_connectSSL before DecoderContext allocation 2672, Fragmentation=13, Thunkstack=0, Free stack=3584, FreeContStack=1472
_connectSSL after DecoderContext allocation 1520, Fragmentation=22, Thunkstack=0, Free stack=3584, FreeContStack=1472
_connectSSL before PrivKey allocation 1520, Fragmentation=22, Thunkstack=0, Free stack=3584, FreeContStack=1472

Exception (29):
epc1=0x4000df64 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

>>>stack>>>

ctx: sys
sp: 3fffec10 end: 3fffffb0 offset: 0190
3fffeda0:  000000c1 3ffee860 00000002 40101210  
3fffedb0:  4025a553 3ffec5a0 00000000 4025a4e8  
3fffedc0:  00000002 4025a48f 00000002 402595e8  
3fffedd0:  40259611 3fffee80 3ffee860 00000016  

Good progress. There is a memory leak, your available memory dropped from 27KB to 8KB and crashed.

Edit: I don't have this memory leak in my version. Make sure to not reallocate a telegramClient object each time.

Initial support for Telegram messaging allows for sending commands to a Tasmota device and receiving response. It also allows sending (user)text from Tasmota to Telegram.

As comms between the two is slow (every message needs at least 1 second to complete connection and transfer) it's more like a gadget than a feature.

Anyway. Enabling Telegram support needs at least uncomment of define USE_TELEGRAM and uncomment of either define USE_TELEGRAM_FINGERPRINT OR define USE_MQTT_TLS_CA_CERT

Default, once enabled in user_config_override it does nothing. It needs to know your bots token and chat id. Enter them with command TmToken and TmChatId.

There are three state commands allowing control over the comms.

  • First to test your connection try TmState 5 allowing for echoing of your bot chat text.
  • Second stop the echo with TmState 4 and enable sending text from Tasmota to telegram with command TmState 1. Now enter on tasmota TmSend Howdy which should report Howdy in telegram.
  • Now in addition to sending from Tasmota you may enable receiving commands from telegram with option TmState 3. Enter any tasmota command in telegram and watch it's response in both tasmota AND in the chat window.

Default polling time is 10 seconds but can be changed with command TmPoll from 4 to 300 seconds.

Complete command syntax can be found in the header of file xdrv_40_telegram.ino

EDIT: Command TmState options are NOT persistent so if needed use rules to keep them in a state you like.

Nice !!!

I really admire you guys. Thanks for this great tasmota software and thanks for this great effort to enable tasmota to send and receive telegram messages. I will test this as soon as possible and share my experience.

I just test this and working well. I can send commands to tasmota and receive messages from tasmota to my telegram account at my phone. Thanks again. I close this request.

I experimented this almost a 3weeks now. I observed that telegramClient->connect("api.telegram.org", 443) is triggering exception 29 whenever my connection to internet is slow. Also, it takes almost 2sec (sometimes 30sec even if I included 1sec timeout in my telegram request String command = "bot" + _token + "/getUpdates?offset=" + offset + "&limit=1&timeout=1";) to connect. Which is why some of the drivers like the webserver is also slow on its response. Can anyone show me a way to solve this? Or is it possible to multithread the process so that I will only process the request result if it is already available and entertain other drivers while waiting for the result.

30s is very long. Do you know if the time is spent at the TLS connection or later at HTTP level?

You could remove comments on line 105, to have a precise timing of TLS connection, and even move to LOG_LEVEL_INFO:

    AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TGM: Connected in %d ms, max ThunkStack used %d"), millis() - tls_connect_time, telegramClient->getMaxThunkStackUse());

AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TGM: Connected in %d ms, max ThunkStack used %d"), millis() - tls_connect_time, telegramClient->getMaxThunkStackUse());

Thanks for the reply @s-hadinger, I actually uncommented that part and it shows ~2000ms, sometimes 30000ms.
18:38:46 TGM: Connected in 21335 ms, max ThunkStack used 4516
TGM: Connected in 1941 ms, max ThunkStack used 4516 --> This is my usual connection about 1800 to 1900+

I also put a log before telegramClient->connect("api.telegram.org", 443) and it is the last log shown whenever an exception 29 triggers.

So it is definitely a problem during TLS connection. 21s is far too long.

You can try to temporarily double buffers, I suspect this may have some side effects (warning it eats up more RAM):

  static const uint32_t tls_rx_size = 4096;
  static const uint32_t tls_tx_size = 2048;

So it is definitely a problem during TLS connection. 21s is far too long.

You can try to temporarily double buffers, I suspect this may have some side effects (warning it eats up more RAM):

  static const uint32_t tls_rx_size = 4096;
  static const uint32_t tls_tx_size = 2048;

Thanks @s-hadinger , i'll try it.
Based on Info: My RAM has 20KB free. When i dont use telegram it would go up to 28KB.

@s-hadinger my RAM went down to 15KB free, but the same performance. Response in 2sec nominal, while sometimes it reached 5sec to 15sec
TGM: Connected in 14571 ms, max ThunkStack used 4484

Ok, it's not the problem then. Logs for bearssl are quite obscure, I don't have any easy solution.

Just to make sure, is the ESP running at 160MHz instead of standard 80 MHz?

Just to note mine runs fine on 80MHz with quick responses around 2 Secs.

Mine also runs at 80Mhz. When I reduce both TLS rx/tx size to 512 it performs much better with 1800ms to 2000ms response. But when internet is slow it goes up to 5sec. I reduce the size to 512 since in my application I only receive up to 10 chars of text with single message each GET.

Good to know that lowering the buffer size actually improves performance. I would still try 160 Mhz.

Hi, great news with the telegram support
but where do I find docu how to configure

thanks for guiding

where do I find docu how to configure

https://github.com/arendst/Tasmota/issues/8619#issuecomment-642764152

where do I find docu how to configure

#8619 (comment)

Hi I thanks

I thought it is enabled in the latest prebuild release.
So do I have to create an own build?

As it's code footprint is large due to TLS support you will need to compile yourselves. You might even need to drop some features to get it even loaded.

Is it possible to send an image by using TmSend method? I have an old foscam lying around and I want to get a snapshot from it by esp and send it to my telegram. I just wanted to make sure if the function supports such image sending.

@ademirci nope. Note enough resources (memory)

hi, can someone explain better how to SEND commands to a tasmota device? I recompiled the bin file adding the 2 use_telegram* directives, all fine, i got all the needed commands... i followed instructions on https://github.com/arendst/Tasmota/issues/8619#issuecomment-642764152 link and succeeded in sending text via tmsend command to the bot, receiving it no problems... even thought the correct chatid is the one of the USER, not the one of the bot itself (which in the end is the part before ":" in the token string)... with MY chat id configured via tmchatid i can do tmsend, while putting in the bot one i get tmsend: failed results... so, given i can SEND text to the bot, and all the tmstate where set to on via "backlog tmstate 1; tmstate 3 ;tmstate 5", which is the correct syntax to send command from the bot TO the tasmota device? I tried "cmnd power 2", "power 2", "cmnd/albero/power 2", and even added a "/" in front of the same commands, nothing appens on tasmota device... thanks in advance :)

hi, flashed a custom 9.0.0.2 (base: standard tasmota.bin config) with the 2 telegram support lines added, on a brand new wemos d1 mini, set as module 18 and with just led1i on d4 and relay1 on d2, nothing else except wifi config... reduced tmpoll to 4, added tmtoken and tmchatid, set tmstate to send and receive ON (not echo), and i can tmsend from device to bot with no problems (just a bit lag), but to receive commands and have them working it's all an other story... i've to intercept the correct instant on which it's going to run its periodical getupdates command to have them arrived on device and have status back as answer... not so reliable, and it's a clean setup... any hint? thanks

Lowering the tmpoll has hardly any use as setting up the connection to telegram takes seconds. The same for sending back information.

To see what you receive enable more logging with seriallog 4.

i've to intercept the correct instant on which it's going to run its periodical getupdates command to have them arrived on device and have status back as answer... not so reliable

In my situation it always receives the command and sends back the result but as it is only polled once every ten seconds it has it's delays. Again this is all caused by setting up a TLS connection to Telegram. Sending one command per ten seconds works fine. More commands are just queued by telegram as Tasmota only handles one at a time for obvious reasons; there is more to do than servicing telegram.

nope, it does not even recognize with sure result a message every 20 seconds... weblog 4 reports very low info, it seems i get a valid command action and response back only if i succeed to sent a command just before a getstatus appears in console... i sent about 20 "power 2" commands from tg bot chat, waiting even 20 or 30 seconds after a single one, or increasing "rate" to as fast i can type on phone, about 1 message every 2 seconds, it works only as i said... logs:

20:58:29 WIF: Checking connection...
20:58:38 CMD: tmstate
20:58:38 SRC: WebConsole from 192.168.1.57
20:58:38 CMD: Group 0, Index 1, Command "TMSTATE", Data ""
20:58:38 RSL: stat/tasmota_7E0E3E/RESULT = {"TmState":{"Send":"OFF","Receive":"OFF","Echo":"OFF"}}
20:58:46 CMD: tmstate 1
20:58:46 SRC: WebConsole from 192.168.1.57
20:58:46 CMD: Group 0, Index 1, Command "TMSTATE", Data "1"
20:58:46 RSL: stat/tasmota_7E0E3E/RESULT = {"TmState":{"Send":"ON","Receive":"OFF","Echo":"OFF"}}
20:58:49 WIF: Checking connection...
20:58:49 CMD: tmstate 3
20:58:49 SRC: WebConsole from 192.168.1.57
20:58:49 CMD: Group 0, Index 1, Command "TMSTATE", Data "3"
20:58:49 RSL: stat/tasmota_7E0E3E/RESULT = {"TmState":{"Send":"ON","Receive":"ON","Echo":"OFF"}}
20:58:50 TGM: Started
20:58:51 TGM: getUpdates
20:58:53 TGM: Response {"ok":true,"result":[]}
20:58:53 CMD: tmstate
20:58:53 SRC: WebConsole from 192.168.1.57
20:58:53 CMD: Group 0, Index 1, Command "TMSTATE", Data ""
20:58:53 RSL: stat/tasmota_7E0E3E/RESULT = {"TmState":{"Send":"ON","Receive":"ON","Echo":"OFF"}}
20:59:02 CMD: tmtoken
20:59:02 SRC: WebConsole from 192.168.1.57
20:59:02 CMD: Group 0, Index 1, Command "TMTOKEN", Data ""
20:59:02 RSL: stat/tasmota_7E0E3E/RESULT = {"TmToken":"105XXXX330:AAHLvwnvXXXXXXXXXXXXXXXXvdx-1kHESyo"}
20:59:05 TGM: getUpdates
20:59:06 TGM: Response {"ok":true,"result":[]}
20:59:06 CMD: tmchatid
20:59:06 SRC: WebConsole from 192.168.1.57
20:59:06 CMD: Group 0, Index 1, Command "TMCHATID", Data ""
20:59:06 RSL: stat/tasmota_7E0E3E/RESULT = {"TmChatId":"33XXXXX63"}
20:59:10 CMD: tmsend test
20:59:10 SRC: WebConsole from 192.168.1.57
20:59:10 CMD: Group 0, Index 1, Command "TMSEND", Data "test"
20:59:10 TGM: sendMessage
20:59:11 RSL: stat/tasmota_7E0E3E/RESULT = {"TmSend":"Done"}
20:59:14 WIF: Checking connection...
20:59:18 TGM: getUpdates
20:59:19 TGM: Response {"ok":true,"result":[]}
20:59:31 TGM: getUpdates
20:59:32 TGM: Response {"ok":true,"result":[]}
20:59:36 WIF: Checking connection...
20:59:44 TGM: getUpdates
20:59:45 TGM: Response {"ok":true,"result":[]}
20:59:57 WIF: Checking connection...
20:59:57 TGM: getUpdates
20:59:58 TGM: Response {"ok":true,"result":[]}
21:00:10 TGM: getUpdates
21:00:11 TGM: Response {"ok":true,"result":[]}
21:00:19 WIF: Checking connection...
21:00:20 CMD: weblog
21:00:20 SRC: WebConsole from 192.168.1.57
21:00:20 CMD: Group 0, Index 1, Command "WEBLOG", Data ""
21:00:20 RSL: stat/tasmota_7E0E3E/RESULT = {"WebLog":4}
21:00:23 TGM: getUpdates
21:00:24 TGM: Response {"ok":true,"result":[]}
21:00:36 TGM: getUpdates
21:00:37 TGM: Response {"ok":true,"result":[]}
21:00:41 WIF: Checking connection...
21:00:49 TGM: getUpdates
21:00:50 TGM: Response {"ok":true,"result":[]}
21:01:02 TGM: getUpdates
21:01:03 TGM: Response {"ok":true,"result":[]}
21:01:04 WIF: Checking connection...
21:01:15 TGM: getUpdates
21:01:16 TGM: Response {"ok":true,"result":[]}
21:01:25 WIF: Checking connection...
21:01:28 TGM: getUpdates
21:01:29 TGM: Response {"ok":true,"result":[{"update_id":xxxxxx,
"message":{"message_id":247,"from":{"id":33XXXXX63,"is_bot":false,"first_name":"xxxxxx","last_name":"xxxxxxx","username":"xxxxxxx","language_code":"it"},"chat":{"id":33XXXXX63,"first_name":"xxxxx","last_name":"xxxxxxx","username":"xxxxxxxx","type":"private"},"date":1602792069,"text":"Power 2"}}]}
21:01:29 TGM: Parsed update_id xxxxxxxxxxx, chat_id 33XXXXX63, text "Power 2"
21:01:29 SRC: Chat
21:01:29 CMD: Group 0, Index 1, Command "POWER", Data "2"
21:01:29 RSL: stat/tasmota_7E0E3E/RESULT = {"POWER":"ON"}
21:01:29 RSL: stat/tasmota_7E0E3E/POWER = ON
21:01:29 TGM: sendMessage
21:01:31 CFG: Saved to flash at F6, Count 30, Bytes 4096
21:01:41 TGM: getUpdates
21:01:44 TGM: Response 
21:01:50 WIF: Checking connection...
21:01:55 RSL: tele/tasmota_7E0E3E/STATE = {"Time":"2020-10-15T21:01:55","Uptime":"0T10:15:24","UptimeSec":36924,"Heap":20,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":0,"POWER":"ON","Wifi":{"AP":1,"SSId":"SharkIOT","BSSId":"20:28:18:A0:F0:42","Channel":11,"RSSI":70,"Signal":-65,"LinkCount":1,"Downtime":"0T00:00:06"}}
21:01:55 TGM: getUpdates
21:01:56 TGM: Response {"ok":true,"result":[]}

I've done the same with the default tasmota.bin and just enabled define USE_TELEGRAM in my_user_config.h

  • tmchatid is empty
  • tmtoken is set correctly
  • tmstate is set to 3
  • entering power 2 on the phone performs just that (after at max 13 seconds as that's the default tasmota telegram poll timeout).

See output below:

11:11:43 CMD: tmchatid
11:11:43 SRC: Serial
11:11:43 CMD: Group 0, Index 1, Command "TMCHATID", Data ""
11:11:43 MQT: stat/wemos4/RESULT = {"TmChatId":""}
11:12:25 CMD: tmtoken
11:12:25 SRC: Serial
11:12:25 CMD: Group 0, Index 1, Command "TMTOKEN", Data ""
11:12:25 MQT: stat/wemos4/RESULT = {"TmToken":""}
11:12:30 CMD: tmtoken 1xxxxxxx08:AyyyyyyyyyyyyyyyzzzxxxxzzzzzzzzzzzM
11:12:30 SRC: Serial
11:12:30 CMD: Group 0, Index 1, Command "TMTOKEN", Data "1xxxxxxx08:AyyyyyyyyyyyyyyyzzzxxxxzzzzzzzzzzzM"
11:12:30 CFG: CR 398/699, Busy 0
11:12:30 MQT: stat/wemos4/RESULT = {"TmToken":"1xxxxxxx08:AyyyyyyyyyyyyyyyzzzxxxxzzzzzzzzzzzM"}
11:12:31 CFG: Saved to flash at F9, Count 79, Bytes 4096
11:12:40 TGM: Started
11:12:41 CMD: tmstate
11:12:41 SRC: Serial
11:12:41 CMD: Group 0, Index 1, Command "TMSTATE", Data ""
11:12:41 MQT: stat/wemos4/RESULT = {"TmState":{"Send":"OFF","Receive":"ON","Echo":"OFF"}}

on phone in chat: status 3

11:13:07 SRC: Chat
11:13:07 CMD: Group 0, Index 1, Command "STATUS", Data "3"
11:13:07 MQT: stat/wemos4/STATUS3 = {"StatusLOG":{"SerialLog":3,"WebLog":2,"MqttLog":0,"SysLog":0,"LogHost":"","LogPort":514,"SSId":["indebuurt_IoT",""],"TelePeriod":300,"Resolution":"558180C0","SetOption":["00008009","2805FA000100060000005A00000000000000","00000000","00006000","00000000"]}}

on phone in chat: power 2

11:13:46 SRC: Chat
11:13:46 CMD: Group 0, Index 1, Command "POWER", Data "2"
11:13:46 MQT: stat/wemos4/RESULT = {"POWER":"ON"}
11:13:46 MQT: stat/wemos4/POWER = ON
11:13:47 CFG: Saved to flash at F8, Count 80, Bytes 4096

on phone in chat: power 2

11:14:12 SRC: Chat
11:14:12 CMD: Group 0, Index 1, Command "POWER", Data "2"
11:14:12 MQT: stat/wemos4/RESULT = {"POWER":"OFF"}
11:14:12 MQT: stat/wemos4/POWER = OFF
11:14:14 CFG: Saved to flash at F7, Count 81, Bytes 4096
11:14:34 CMD: tmchatid
11:14:34 SRC: Serial
11:14:34 CMD: Group 0, Index 1, Command "TMCHATID", Data ""
11:14:34 MQT: stat/wemos4/RESULT = {"TmChatId":""}

nothing here... did as you say (removed tmchatid and left only tmtoken, and just tmstate 3), i waited even more than 30 seconds, i can get it work only if i send the command just before a "getupdates" appears in web console... i'll go for nodered at this point, i can get faster and sure results and i can block access to unknown src chatids... thanks anyway :)

nothing here... did as you say (removed tmchatid and left only tmtoken, and just tmstate 3), i waited even more than 30 seconds, i can get it work only if i send the command just before a "getupdates" appears in web console... i'll go for nodered at this point, i can get faster and sure results and i can block access to unknown src chatids... thanks anyway :)

Maybe studying how telegram works might help you better understand your problem. You may also add log to enhance your debugging.

i just know that with other platforms or "addons" like nodered, telegram messaging is instantly answering... but i understand what @arendst said about tls and the fact that tasmota has to do other stuff, too... there's no much logs to share if i don't get anything useful from them, but here they are, again... i mixed a couple of comments inside the logs themselves...

00:00:00 CFG: Loaded from flash at F7, Count 37
00:00:00 QPC: Count 1
00:00:00 CFG: CR 385/699, Busy 0
00:00:00 SRC: Restart
00:00:00 Project tasmota Tasmota Version 9.0.0.2(tasmota)-2_7_4_3
00:00:00 WIF: Checking connection...
00:00:00 WIF: Attempting connection...
00:00:00 WIF: Connecting to AP1 SharkIOT Channel 11 BSSId 20:28:18:A0:F0:42 in mode 11N as tasmota_7E0E3E-3646...
00:00:01 WIF: Checking connection...
00:00:01 WIF: Attempting connection...
00:00:02 WIF: Checking connection...
00:00:02 WIF: Attempting connection...
00:00:03 WIF: Checking connection...
00:00:03 WIF: Attempting connection...
00:00:04 WIF: Checking connection...
00:00:04 WIF: Attempting connection...
00:00:05 WIF: Checking connection...
00:00:05 WIF: Attempting connection...
00:00:06 QPC: Reset
00:00:06 WIF: Checking connection...
00:00:06 WIF: Connected
00:00:07 HTP: Web server active on tasmota_7E0E3E-3646 with IP address 192.168.1.123
16:06:42 NTP: UTC 2020-10-17T15:06:41, DST 2020-03-29T02:00:00, STD 2020-10-25T03:00:00
16:06:42 RSL: tele/tasmota_7E0E3E/INFO1 = {"Module":"Generic","Version":"9.0.0.2(tasmota)","FallbackTopic":"cmnd/DVES_7E0E3E_fb/","GroupTopic":"cmnd/tasmotas/"}
16:06:42 RSL: tele/tasmota_7E0E3E/INFO2 = {"WebServerMode":"Admin","Hostname":"tasmota_7E0E3E-3646","IPAddress":"192.168.1.123"}
16:06:42 RSL: tele/tasmota_7E0E3E/INFO3 = {"RestartReason":"External System"}
16:06:42 RSL: stat/tasmota_7E0E3E/RESULT = {"POWER":"OFF"}
16:06:42 RSL: stat/tasmota_7E0E3E/POWER = OFF
16:06:42 APP: Boot Count 7
16:06:42 CFG: Saved to flash at F6, Count 38, Bytes 4096
16:06:43 RSL: tasmota/discovery/DC4F227E0E3E/config = {"ip":"192.168.1.123","dn":"Tasmota","fn":["Tasmota",null,null,null,null,null,null,null],"hn":"tasmota_7E0E3E-3646","mac":"DC4F227E0E3E","md":"Generic","ty":0,"ofln":"Offline","onln":"Online","state":["OFF","ON","TOGGLE","HOLD"],"sw":"9.0.0.2","t":"tasmota_7E0E3E","ft":"%prefix%/%topic%/","tp":["cmnd","stat","tele"],"rl":[1,0,0,0,0,0,0,0],"swc":[-1,-1,-1,-1,-1,-1,-1,-1],"btn":[0,0,0,0],"so":{"11":0,"13":0,"17":0,"20":0,"30":0,"68":0,"73":0,"80":0,"82":0},"lk":1,"lt_st":0,"ver":1}
16:06:43 RSL: tasmota/discovery/DC4F227E0E3E/sensors = {"sn":{"Time":"2020-10-17T16:06:43"},"ver":1}
16:06:46 RSL: tele/tasmota_7E0E3E/STATE = {"Time":"2020-10-17T16:06:46","Uptime":"0T00:00:14","UptimeSec":14,"Heap":27,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":0,"POWER":"OFF","Wifi":{"AP":1,"SSId":"SharkIOT","BSSId":"20:28:18:A0:F0:42","Channel":11,"RSSI":86,"Signal":-57,"LinkCount":1,"Downtime":"0T00:00:08"}}
16:07:00 WIF: Checking connection...
16:07:20 WIF: Checking connection...
16:07:40 WIF: Checking connection...
16:08:00 WIF: Checking connection...
16:08:20 WIF: Checking connection...
16:08:40 WIF: Checking connection...
16:09:00 WIF: Checking connection...
16:09:17 HTP: Main Menu
16:09:20 SRC: WebGui from 192.168.1.57
16:09:20 RSL: stat/tasmota_7E0E3E/RESULT = {"POWER":"ON"}
16:09:20 RSL: stat/tasmota_7E0E3E/POWER = ON
16:09:20 CFG: Saved to flash at F5, Count 39, Bytes 4096
16:09:20 WIF: Checking connection...
16:09:21 SRC: WebGui from 192.168.1.57
16:09:21 RSL: stat/tasmota_7E0E3E/RESULT = {"POWER":"OFF"}
16:09:21 RSL: stat/tasmota_7E0E3E/POWER = OFF
16:09:21 CFG: Saved to flash at F4, Count 40, Bytes 4096
16:09:23 HTP: Console
16:09:27 CMD: weblog 4
16:09:27 SRC: WebConsole from 192.168.1.57
16:09:27 CMD: Group 0, Index 1, Command "WEBLOG", Data "4"
16:09:27 RSL: stat/tasmota_7E0E3E/RESULT = {"WebLog":4}
16:09:31 CMD: tmchatid
16:09:31 SRC: WebConsole from 192.168.1.57
16:09:31 CMD: Group 0, Index 1, Command "TMCHATID", Data ""
16:09:31 RSL: stat/tasmota_7E0E3E/RESULT = {"TmChatId":""}
16:09:34 CMD: tmtoken
16:09:34 SRC: WebConsole from 192.168.1.57
16:09:34 CMD: Group 0, Index 1, Command "TMTOKEN", Data ""
16:09:34 RSL: stat/tasmota_7E0E3E/RESULT = {"TmToken":"10xxxxxx330:AAHLvwnvcNhQxxxxxxxxxxdx-1kHESyo"}
16:09:37 CMD: tmstate 3
16:09:37 SRC: WebConsole from 192.168.1.57
16:09:37 CMD: Group 0, Index 1, Command "TMSTATE", Data "3"
16:09:37 RSL: stat/tasmota_7E0E3E/RESULT = {"TmState":{"Send":"OFF","Receive":"ON","Echo":"OFF"}}
16:09:38 TGM: Started
16:09:39 TGM: getUpdates
16:09:40 TGM: Response {"ok":true,"result":[]}
16:09:42 WIF: Checking connection...
16:09:52 TGM: getUpdates
16:09:53 TGM: Response {"ok":true,"result":[]}
16:10:03 WIF: Checking connection...
16:10:05 TGM: getUpdates

# in this specific moment i sent "power 2" via telegram from phone... nothing happened for about a minute...

16:10:06 TGM: Response {"ok":true,"result":[]}
16:10:18 TGM: getUpdates
16:10:19 TGM: Response {"ok":true,"result":[]}
16:10:26 WIF: Checking connection...
16:10:31 TGM: getUpdates
16:10:32 TGM: Response {"ok":true,"result":[]}
16:10:44 TGM: getUpdates
16:10:45 TGM: Response {"ok":true,"result":[]}
16:10:48 WIF: Checking connection...
16:10:57 TGM: getUpdates
16:10:58 TGM: Response {"ok":true,"result":[]}
16:11:09 WIF: Checking connection...
16:11:10 TGM: getUpdates
16:11:11 TGM: Response {"ok":true,"result":[]}
16:11:23 TGM: getUpdates
16:11:24 TGM: Response {"ok":true,"result":[]}
16:11:32 WIF: Checking connection...
16:11:36 TGM: getUpdates

# in this moments i was sending "power 2" every 15 seconds... when i succeeded to send one right before this "getUpdates" here above, then i finally got a single working message back and command executed, here below... it's the only success as you can see...

16:11:37 TGM: Response {"ok":true,"result":[{"update_id":850501108,
"message":{"message_id":304,"from":{"id":xxxxxxxxx,"is_bot":false,"first_name":"xxxxxxx","last_name":"xxxxxx","username":"xxxxxx","language_code":"it"},"chat":{"id":xxxxxxx,"first_name":"xxxxxx","last_name":"xxxxxx","xxxxx":"xxxxxx","type":"private"},"date":1602947480,"text":"Power 2"}}]}
16:11:37 TGM: Parsed update_id 850501108, chat_id xxxxxxx, text "Power 2"
16:11:37 SRC: Chat
16:11:37 CMD: Group 0, Index 1, Command "POWER", Data "2"
16:11:37 RSL: stat/tasmota_7E0E3E/RESULT = {"POWER":"ON"}
16:11:37 RSL: stat/tasmota_7E0E3E/POWER = ON
16:11:37 TGM: sendMessage
16:11:39 CFG: Saved to flash at FB, Count 41, Bytes 4096
16:11:46 RSL: tele/tasmota_7E0E3E/STATE = {"Time":"2020-10-17T16:11:46","Uptime":"0T00:05:14","UptimeSec":314,"Heap":20,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":0,"POWER":"ON","Wifi":{"AP":1,"SSId":"SharkIOT","BSSId":"20:28:18:A0:F0:42","Channel":11,"RSSI":80,"Signal":-60,"LinkCount":1,"Downtime":"0T00:00:08"}}
16:11:49 TGM: getUpdates
16:11:52 TGM: Response 
16:11:57 WIF: Checking connection...

You may check the status of your bot via typing the api commands (https://api.telegram.org/bot<token>/METHOD_NAME)
example: (to get all unread messages)
https://api.telegram.org/bot<token>/getUpdates?offset=<offset>
by this, you can see unread messages that is why you don't get what you expecting. If some message is unread, you cannot receive the current message you sent. Especially if your offset is higher than that of your current message's updateID
This is true if you send special bot commands that starts with "/" such as /start /getMe etc. Your bot will receive distinct message format when you use these special bot commands but will retain/follow the updateID trend.

You should receive this on your browser if you have no queued/unread messages:
{"ok":true,"result":[]}

For more details: https://core.telegram.org/bots/api

Hello, I have some questions.
It is posible to report the sensors value to Telegram upon change or using the periodic telemetry data with TelePeriod and PowerDelta like with MQTT?
For instance, for the Sonoff Pow R2 the message in Telegram: Power 234 W
and for the Sonoff TH in Telegram the message in Telegram: Temperature 25,6掳 C
And of course, how can I do that?
thanks

Hello, I have some questions.
It is posible to report the sensors value to Telegram upon change or using the periodic telemetry data with TelePeriod and PowerDelta like with MQTT?
For instance, for the Sonoff Pow R2 the message in Telegram: Power 234 W
and for the Sonoff TH in Telegram the message in Telegram: Temperature 25,6掳 C
And of course, how can I do that?
thanks

First of all a big thank you to everyone contributing to this feature, I have been really looking forward to this and have been testing a lot with WEBSEND command before I found this thread.
I do have the same question as Boris17210. I managed to flash the latest Tasmota release with Telegram support following the description of https://github.com/arendst/Tasmota/issues/8619#issuecomment-64276415 and can now receive messages from my bot and see them in the web console as well as sending telegram messages.

The use case I have in mind is regarding my washing machine. I would like to monitor the power consumption and once it is done send a telegram message. Within the the message it would be great to include some status information such as required ,time consumed energy etc. Is it possible to achieve something like this using TmSend command?

Big Thanks also from me for this nice feature. This helps to let Tasmota device run as standalone devices. There are so many use cases for this where you just need a state update instantly on your device when a rule is executed.

I've just tested it with a self compiled and a binary from ota page and both works perfectly. Thank you so much.

Great Work

Is there a step by step guide on how to enable Telegram on a newly flashed Tasmota deive? That would be extremely helpful. Many thanks!

Hello, there is no documentation at all but here are stepts to get telegram running on a Tasmota device.

Prerequisites:
Installed Git -> https://git-scm.com/downloads
Installed VSCode with Platform.io Plugin -> https://code.visualstudio.com/download and https://platformio.org/install/ide?install=vscode

Checkout the code with git somewhere on your disk with:

git checkout https://github.com/arendst/Tasmota.git

Then Open VsCode and click on File -> Open Folder (Choose the Folder where Tasmota was checked out)
Then open the File my_user_config.h under tasmota folder
Edit the file my_user_config.h -> Find // #define USE_TELEGRAM and remove the //
This enables the Telegram feature.

Then compile by choosing Platform.io (The little alien picture at the bottom of the left bar) and choose a project task for your needs (Best would be to choose env:tasmota-YOUR-LANGUAGE)
Click on it and choose General -> Build

After the task has completed you have a Telegram enabled binary and gzipped binary in your tasmota folder under build_output/firmware.

You can then use this to flash your Tasmota device manually. If your device don't have Tasmota, erase the flash and flash it with Tasmotizer.
If you already have Tasmota on it, you can do an OTA update but you have to update to the minimal firmware first and then to your built gzipped binary file.

To enable Telegram you have to chat to the Botfather and get an own Telegram Bot with a token and a chatId. How you can achieve this -> here: https://www.toptal.com/python/telegram-bot-tutorial-python
To get the ChatId type into your Bots chat the command: /getUpdates
This will give you a JSON String where you can find your chatId.

Then you have to navigate to your Tasmota device and type the following into your console:
tmtoken YOUR BOT TOKEN
tmchatid YOUR BOTS CHATID
tmstate 1 //to enable sending telegram message
tmstate 3 //to enable receiving telegram messages in your Tasmota device
tmstate 5 //to enable echo your telegram messages/commands

tmstate 0, tmstate 2 and tmstate 4 is for disabling the above features

if everything works right then you are able to use the command
tmsend WITH YOUR MESSAGE TEXT HERE

Example

tmtoken sofdih98we:099we000we脽0we8ru09uew08zrehfjc
tmchatid 12343216453
tmstate 1
tmstate 3
tmstate 5
tmsend Hello out there. I'm using Telegram now!

I Hope this helps also others to get their devices into telgram.

Again Thanks Theo for this great feature - feel to take all or parts of this little tutorial for your documentation ;)

Wow, thank you so much for the detailed information!

I'v seen that my quoted variables were not visible to you - so I've updated the post. Please reread it and if you have any further questions -> just ask.

Hello, I have some questions.
It is posible to report the sensors value to Telegram upon change or using the periodic telemetry data with TelePeriod and PowerDelta like with MQTT?
For instance, for the Sonoff Pow R2 the message in Telegram: Power 234 W
and for the Sonoff TH in Telegram the message in Telegram: Temperature 25,6掳 C
And of course, how can I do that?
thanks

First of all a big thank you to everyone contributing to this feature, I have been really looking forward to this and have been testing a lot with WEBSEND command before I found this thread.
I do have the same question as Boris17210. I managed to flash the latest Tasmota release with Telegram support following the description of #8619 (comment) and can now receive messages from my bot and see them in the web console as well as sending telegram messages.

The use case I have in mind is regarding my washing machine. I would like to monitor the power consumption and once it is done send a telegram message. Within the the message it would be great to include some status information such as required ,time consumed energy etc. Is it possible to achieve something like this using TmSend command?

Hi to All!
And a BiG Thanks for your work!!
Me too I'm interested how got sensors data from device to telegram chat!
I have to use a rule to got sensors data?
I know Tasmota rules but I don't how to start for this... Can someone help me please? Can someone address me to start?
Thank you so much for your help!

I'v seen that my quoted variables were not visible to you - so I've updated the post. Please reread it and if you have any further questions -> just ask.

hi, just tested again and the problem for me is always the same... i can "tmsend something" from tasmota to the bot, but getting something back is rare if not unique... i send from the bot "status 2" for example, wait for 30 seconds, and i never get back anything... if i continue to write "status 2" repeatdly and i get the correct timing when the console is doing "checking connection", i randomly succeed to get the answer... but it's RARE...

I'v seen that my quoted variables were not visible to you - so I've updated the post. Please reread it and if you have any further questions -> just ask.

hi, just tested again and the problem for me is always the same... i can "tmsend something" from tasmota to the bot, but getting something back is rare if not unique... i send from the bot "status 2" for example, wait for 30 seconds, and i never get back anything... if i continue to write "status 2" repeatdly and i get the correct timing when the console is doing "checking connection", i randomly succeed to get the answer... but it's RARE...

Hi,

that could be an issue because the polling interval Tasmota polls the Telegram service defaults to 10 seconds and cannot be less than 10 secs. So I guess you have to wait for an answer almost 10 seconds.

But you can check if everything is configured the right way by typing the following commands:

tmtoken
you should get a JSON object with you token in it

tmchatid
you should get a JSON object with your chat id in it

tmstate
you should get this: {"TmState":{"Send":"ON","Receive":"ON","Echo":"ON"}}

@scheilch yup, all configured good, just checked your commands... if you scroll you'll see these are issues i had already, i know about that polling time, as said i waited even 30 seconds, but getting something back is rare... i'll go for nodered without using these, it's easier and VERY fast...

Was this page helpful?
0 / 5 - 0 ratings