Telegram and Annex32

Place code snippets and demo code here
Post Reply
User avatar
cicciocb
Site Admin
Posts: 1899
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Telegram and Annex32

Post by cicciocb »

Hi All, this is a simple example of how interface telegram with Annex32.

First of all you need a BOT TOKEN that can be obtained following the instructions found here : How to get Telegram bot API token

Then, using the following snippets, it will be possible to get and send messages to telegram.
Using WGET$ (or even better WGETASYNC) it is possible to check regularly if new messages have been received and execute the corresponding actions.

cicciocb

Code: [Local Link Removed for Guests]

'Telegram example how to interface with Annex32
'simple commands using the BOT
'requires the BOT TOKEN and the CHAT ID
BOT_TOKEN$ = "1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
CHAT_ID$ = "1234567890"

'CHECK IF THE BOT TOKEN IS CORRECT (just use once to confirm that it is working)
a$ = "https://api.telegram.org/bot" + BOT_TOKEN$ + "/getMe"
wlog wget$(a$)

'CHECK MESSAGES
a$ = "https://api.telegram.org/bot" + BOT_TOKEN$ + "/getUpdates"
b$ = wget$(a$)
wlog b$
wlog "Message : "; json$(b$, "message.text")
id$ = json$(b$, "update_id")
wlog "update_id :"; id$
wlog "Chat_id :"; json$(b$, "chat.id")

' defines the id for the next message
next_id$ = trim$(str$(val(id$) + 1, "%11.0f"))

'CHECK MESSAGES WITH OFFSET
a$ = "https://api.telegram.org/bot" + BOT_TOKEN$ + "/getUpdates?offset=" + next_id$
b$ = wget$(a$)
wlog b$
wlog "Message : "; json$(b$, "message.text")

'SEND WITH GET
msg$ = "This is my Telegram message"
a$ = "https://api.telegram.org/bot" + BOT_TOKEN$ + "/sendMessage?chat_id=" + CHAT_ID$ + "&text=" + msg$
wlog wget$(a$)
User avatar
cicciocb
Site Admin
Posts: 1899
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: Telegram and Annex32

Post by cicciocb »

This is another example of how use Telegram with Annex32.
This example uses the WGETASYNC command so the program can continue normally without waiting for the Telegram messages.
The Telegram bot is read each 5 seconds and, is a message is received, it is replied back.
In parallel, the program prints a number sequence in the console just to simulate that all happens in background.

cicciocb

Code: [Local Link Removed for Guests]

'Telegram example on how receive messages from Telegram
'Simple Echo demo: Reply with the message received
'requires the BOT TOKEN
'BOT_TOKEN$ = "1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"

onwgetasync asynco
timer0 5000, check_telegram
next_id$ = ""

'simulate program activity
z = 0
while 1
  print z
  z = z + 1
  pause 100 
wend

wait

check_telegram:
wgetasync "https://api.telegram.org/bot" + BOT_TOKEN$ + "/getUpdates?offset=" + next_id$
return

asynco:
b$ = WGETRESULT$
msg$ = json$(b$, "message.text")
if (msg$ <> "not found") then
  id$ = json$(b$, "update_id")
  next_id$ = trim$(str$(val(id$) + 1, "%11.0f")) ' set the id for the next message
  CHAT_ID$ = json$(b$, "chat.id")
  wlog "Message : "; msg$
  wlog "Chat ID : "; CHAT_ID$  
  wlog "From :"; json$(b$, "username")  
  
  'Reply message
  msg$ = "You sent me: " + msg$
  wlog wget$("https://api.telegram.org/bot" + BOT_TOKEN$ + "/sendMessage?chat_id=" + CHAT_ID$ + "&text=" + msg$)
end if
return
Post Reply