Page 1 of 1

Rules + HttpToSend + Telegram ?

Posted: 05 Mar 2020, 14:31
by spachal
Hello, I need your help :]. I'm trying to make some notifications from espeasy to mobile phone and send messages (notifications) to Telegram.
I have userID and API token, sending messages from browser to Telegram client is without any problem, URL is simple:

Code: Select all

http://api.telegram.org/bot<token>/sendMessage?chat_id=<userID>&text=<text_I_want_to_send>
FYI, here are two articles how to do that, it's simple (but to get userID instead of nickname is a little bit tricky): https://cutt.ly/jtrhbIt & https://cutt.ly/mtrhvan

But now I'm trying to send any message from espeasy. Configuration is as simple as possible, here is Rule#1 code, every 5 seconds it should send message to TG:

Code: Select all

on System#Boot do
 TimerSet,1,5
endon

On Rules#Timer=1 do
 SendToHTTP,api.telegram.org,80,/botxxxxxx/sendMessage?chat_id=xxxxxx&text=testmessage
 TimerSet,1,5
endon
(I only replaced APItoken & userID with xxxxxx here)

I know that there is difference between HTTP and HTTPS, but URL for posting message is working also on port 80, I tried that in browsers, without any problem.
Here is log from esp:

Code: Select all

10698834:      : Rebooting...
6390: Subscribed to: ESP008/#
6401: EVENT: WiFi#Connected
6443: EVENT: Rules#Timer=1
6452: ACT  : SendToHTTP,api.telegram.org,80,/botxxxxxx/sendMessage?chat_id=xxxxxx&text=testmessage
6455: Command: SendToHTTP
6456: SendToHTTP: Host: api.telegram.org port: 80
6730: ACT  : TimerSet,1,5
6732: Command: TimerSet
So it seems that rules and SendToHTTP command works well. But no messages are received.

I tried a lot of combinations, also with URL shorteners (to avoid case sensitive and URL lenght possible problems), with IP instead of domain name, with spaces and without them, put URL in apostrofs (as it is mentioned in wiki) .. all the same, no message sent to TG.
There are probably no problems with ESP communication outside (I'm using also Thingspeak.com etc.). I'm using NTP, I also set IP/mask/gw/dns manually, I suppose that communication to internet is working. If I changed port to 443 for test, I got error message (HTTP : Command_HTTP_SendToHTTP Error: HTTP/1.1 400 Bad Request^), so internet connection is working.

SendToHTTP commnad is also probably working, I tried a simple rule to flash LED (GPIO13) on second ESP8266, it works:

Code: Select all

on System#Boot do
 TimerSet,1,10
endon

On Rules#Timer=1 do
 SendToHTTP,<ip>,80,'/control?cmd=GPIO,13,1'
 TimerSet,2,2
endon

On Rules#Timer=2 do
 SendToHTTP,<ip>,80,'/control?cmd=GPIO,13,0'
 TimerSet,3,2
endon

On Rules#Timer=3 do
 SendToHTTP,api.telegram.org,80,/botxxxxxx/sendMessage?chat_id=xxxxxx&text=testmessage
 TimerSet,1,2
endon
LED is flashing, but messages to TG are not working.

Code: Select all

912433: Command: TimerSet
915237: EVENT: Rules#Timer=1
915246: ACT  : SendToHTTP,<ip>,80,'/control?cmd=GPIO,13,1'
915248: Command: SendToHTTP
915250: SendToHTTP: Host: <ip> port: 80
915286: HTTP : Command_HTTP_SendToHTTP Success! HTTP/1.1 200 OK^
915290: ACT  : TimerSet,2,2
915292: Command: TimerSet
918236: EVENT: Rules#Timer=2
918246: ACT  : SendToHTTP,<ip>,80,'/control?cmd=GPIO,13,0'
918248: Command: SendToHTTP
918250: SendToHTTP: Host: <ip> port: 80
918282: HTTP : Command_HTTP_SendToHTTP Success! HTTP/1.1 200 OK^
918285: ACT  : TimerSet,3,2
918287: Command: TimerSet
921236: EVENT: Rules#Timer=3
921249: ACT  : SendToHTTP,api.telegram.org,80,'/botxxxxxxxxx/sendMessage?chat_id=xxxxxxxx&text=te
921252: Command: SendToHTTP
921254: SendToHTTP: Host: api.telegram.org port: 80
921581: ACT  : TimerSet,1,2
921583: Command: TimerSet
hw: LOLIN D1 mini,
fw: mega-20200222
i tried also old and new rules engine, Tolerant last parameter, SendToHTTP wait for ack .. all the same.

I suppose that there is some simple mistake from my side, can you help me to solve this?
IMHO using Telegram as instant ('push') notifications via URL could be great, there are many clients for different platforms for free. But it must be functional :].

Thanks! :]

Re: Rules + HttpToSend + Telegram ?

Posted: 05 Mar 2020, 16:19
by grovkillen
Does Telegram allow for connections over HTTP? I thought it was only HTTPS on their part.

Re: Rules + HttpToSend + Telegram ?

Posted: 06 Mar 2020, 00:48
by spachal
Hi, I don't know how it works .. If I send URL like this one via browser, I receive Telegram message without problem.

Code: Select all

http://api.telegram.org:80/bot.....
In this article are also some informations about HTTP protocol: https://core.telegram.org/mtproto/transports#http
And I know that it's possible to use ESP8266 with Telegram also, for example ESP8266TelegramBOT.h library etc.

But, all of this is over my knowledge, that's why I'm asking here, if it's possible to do that in espeasy or not :].
I have always a possibility to transfer message to node-red over mqtt a send it from node-red, but it should be much easier to send it directly from ESP :].

Re: Rules + HttpToSend + Telegram ?

Posted: 06 Mar 2020, 01:35
by ThomasB
If I send URL like this one via browser, I receive Telegram message without problem.
Are you sure the browser isn't redirecting the http to https?
And I know that it's possible to use ESP8266 with Telegram also, for example ESP8266TelegramBOT.h library etc.
The Arduino code uses SSL / port 443.

Telegram boasts about security. So it seems odd they would tolerate http connections. And I cannot find any working user examples on sending a message via http. But, maybe it's possible and the problem is due to your rule's URI syntax.

I have some alternate examples for you to try:

Example #1

Code: Select all

 SendToHTTP,api.telegram.org,80,/botxxxxxx/sendMessage?chat_id=xxxxxx&amp;text=testmessage
Example #2

Code: Select all

 SendToHTTP,api.telegram.org,80,'/botxxxxxx/sendMessage?chat_id=xxxxxx&amp;text=testmessage'
Example #3

Code: Select all

 SendToHTTP,api.telegram.org,80,'/botxxxxxx/sendMessage?chat_id=xxxxxx&text=testmessage'
- Thomas

Re: Rules + HttpToSend + Telegram ?

Posted: 06 Mar 2020, 12:55
by spachal
Hi Thomas, thanks for your help! :]

I found that URL with port80 is redirected (301) to secure connection in browser automaticaly, so this is probably the root of this problem .. I tried also all mentioned variants, all the same, not working.
I'm going to solve this with mqtt & node-red, but it could be great to have support of Telegram (in Notifications maybe?) directly in espeasy, something like SendToHTTPS?
Just a suggestion, not request :]

Have a great weekend, all!