Switch to desktop version  
Receiving Telegram messages - Printable Version

+- Ewon Technical Forum (https://techforum.ewon.biz)
+-- Forum: Development (https://techforum.ewon.biz/forum-50.html)
+--- Forum: BASIC Script (https://techforum.ewon.biz/forum-52.html)
+--- Thread: Receiving Telegram messages (/thread-1907.html)



Receiving Telegram messages - nico.oosthuizen - 05-01-2022

Hi,

Has anyone managed to receive telegram messages on the ewon flexy similar to how it can receive and respond to SMS's. What I would like to do is to send a keyword to the ewon and it reply with tag info i.e. tank levels


RE: Receiving Telegram messages - Jean-Yves - 06-01-2022

Hi, 

Here is example of working message I have done to send message to Telegram

B$ = "04/08/2021 14:20:39;Tag_1;ALM;LO"
a$="https://api.telegram.org/bot1722691769:AAFZp2HxxxxxxNjGIH-t1XxxxxxqoBmcCY/sendMessage?chat_id=-580000009&text=" + B$
REQUESTHTTPX a$,"GET"




I didn't test yet this for reception myself, but it should work

-from your Telegram account search @BotFather channel in order to create an own "bot"
-sending the message /newbot, it is possible to create the bot (ex test) and the username (ex. EwonBot)
-with the answer from @BotFather wil be available the token in order to use Telegram API (ex. 1416459033:AAFOOVEQGfjyWWCICpkaVSfxSMLYOrhbqxx )
-create a new chat group in order to invite the bot/member (ex. test)
-selecting your group you can retrive the ID also useful to use Telegram API (ex. https://web.telegram.org/#/im?p=g463505211 )
-now you have

$token = "1416459033:AAFOOVEQGfjyWWCICpkaVSfxSMLYOrhbqxx"

$chat = "-463505211" // add “-“ to the ID

So the URL to call is something like that:

https://api.telegram.org/bot$token/sendMessage?chat_id=$chat&text=Hello+World

https://api.telegram.org/bot1416459033:AAFOOVEQGfjyWWCICpkaVSfxSMLYOrhbqxx/sendMessage?chat_id=-463505211&text=Hello+World

JSON answer is:

{
"ok": true,
"result": {
"message_id": 15,
"from": {
"id": 1416459033,
"is_bot": true,
"first_name": "test",
"username": "EwonBot"
},
"chat": {
"id": -463505211,
"title": "EwonTest",
"type": "group",
"all_members_are_administrators": true
},
"date": 1604095842,
"text": "Hello+World"
}
}

With Ewon Basic you can use REST command as below:

REM ****************************************************** 
Trequest:
a$="https://api.telegram.org/bot1416459033:AAFOOVEQGfjyWWCICpkaVSfxSMLYOrhbqxx/sendMessage?chat_id=-463505211&text=Messaggio+da+EwonFlexy"
REQUESTHTTPX a$,"GET"
actionID% = GETSYS PRG, "ACTIONID"
PRINT "request actionid is "; actionID%
ONSTATUS "GOTO TonEvent"
TonEvent:
eventId% = GETSYS PRG, "EVTINFO"
IF (eventId% = actionID%) THEN
SETSYS PRG, "ACTIONID", eventId%
stat% = GETSYS PRG, "ACTIONSTAT"
IF (stat% = 0) THEN
GOTO Tresponse
ELSE
PRINT "Error (ERROR = "+Str$(stat%) + ")"
ENDIF
ENDIF
END

Tresponse:
a$ = RESPONSEHTTPX "STATUSCODE"
PRINT "status: "; a$
a$ = RESPONSEHTTPX "HEADER"
PRINT "all headers: "; a$
a$ = RESPONSEHTTPX "HEADER", "Server"
PRINT "server header: "; a$
a$ = RESPONSEHTTPX "RESPONSE-BODY"
IF (Len(a$) < 1000) THEN
PRINT "response body: "; a$
Else
PRINT "response body size: "; Len(a$)
ENDIF
END
REM ******************************************************  

The answer in the basic console is:
******************************************************
request actionid is 21

connection: close

access-control-allow-methods: GET, POST, OPTIONS

access-control-allow-origin: *

server: nginx/1.16.1

content-type: application/json

server header: nginx/1.16.1

response body: {"ok":true,"result":{"message_id":14,"from":{"id":1416459033,"is_bot":true,"first_name":"test","username":"EwonBot"},"chat":{"id":-463505211,"title":"EwonTest","type":"group","all_members_are_administrators":true},"date":1604093059,"text":"Hello+World"}}
******************************************************