Page 1 of 1

POST HTTP method for Telegram bot

Posted: 25 Oct 2021, 23:07
by amateur+
Aim is to send a message (method sendMessage) or create a poll (method sendPoll) for Telegram bot via Autohotkey.
Messages could contain variables.
Look here: viewtopic.php?p=313052&sid=1631c5cddacad05547b3acac6d761de7#p313052
And here: https://core.telegram.org/bots/api#making-requests
SendMessage: https://core.telegram.org/bots/api#sendmessage
SendPoll: https://core.telegram.org/bots/api#sendpoll

I would use URLDownloadToFile function, but it allows only Latin letters.

Re: POST HTTP method for Telegram bot

Posted: 26 Oct 2021, 04:59
by mikeyww
I did not try your script, but you could save it in UTF-8 with BOM signature. If your Web browser works to download a given URL, then AHK should also work with it.

Re: POST HTTP method for Telegram bot

Posted: 27 Oct 2021, 02:22
by amateur+
Thanks for your reply, mikeyww!
Saving in UTF-8 with BOM signature didn't help URLDownloadToFile function to work with cyrilic letters. With Latin letters everything is ok.
Telegram returns this message:
{"ok":false,"error_code":400,"description":"Bad Request: poll options must be encoded in UTF-8"}

But there is one working way with WinHttp.WinHttpRequest.5.1:

Code: Select all

SomeVariable = test test
 
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
 myurl1 := "https://api.telegram.org/bot<here is bot token>/sendPoll?chat_id=-<here is chat id>&question=Will we live on Mars?`%0A(" . SomeVariable . ")&options=[""Yeah!"",""Nope"",""Blah blah blah""]&is_anonymous=false"
whr.Open("POST", myurl1, true)
; whr.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
whr.Send("")
whr.WaitForResponse()
MsgBox % whr.ResponseText
But if I need to do several actions (for example, create a poll, pin it, delete the message about pining) I have to use this Open() method each time. Could you help me to understand, how can I use this whr.Send('something') instead and using Open() only once?
If I use whr.Open("POST", "https://api.telegram.org/bot<here is bot token>/", true) and then whr.Send("sendMessage?chat_id=-<here is chat id>&text=blahblah") Telegram is angry:
{"ok":false,"error_code":404,"description":"Not Found"}
Even with including whr.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")

Here is a bot for testing if someone needs it (I've created it just now):
@TestAutohotkeybot (t.me/TestAutohotkeybot)
Its token: 2086365146:AAGWZOvm0yFQYU7Dh_wM1pgwrWDC7dsvlK4
https://api.telegram.org/bot2086365146:AAGWZOvm0yFQYU7Dh_wM1pgwrWDC7dsvlK4/
Just create new test group and add this bot to it. To know chat.id it is convinient to add @RawDataBot to the group and it will send the message with "chat": {
"id": -<here is chat id>, ("-" is important).

Re: POST HTTP method for Telegram bot  Topic is solved

Posted: 27 Oct 2021, 04:43
by mikeyww
You could use a function. Adjust as needed.

Code: Select all

whr          := ComObjCreate("WinHttp.WinHttpRequest.5.1")
SomeVariable  = test test
chat_id       = <here is chat id>
question      = Will we live on Mars?`%0A(%SomeVariable%)
options       = ["Yeah!","Nope","Blah blah blah"]

MsgBox, 64, Result, % telegram(whr, chat_id, question, options)

telegram(whr, chat_id, question, options) {
 Static token := "2086365146:AAGWZOvm0yFQYU7Dh_wM1pgwrWDC7dsvlK4"
      , stem  := "https://api.telegram.org/bot" token
      , anon  := "false"
 url := stem "/sendPoll?chat_id=-" chat_id "&question=" question "&options=" options "&is_anonymous=" anon
 whr.Open("POST", url, true), whr.Send(), whr.WaitForResponse()
 Return whr.ResponseText
}

Re: POST HTTP method for Telegram bot

Posted: 27 Oct 2021, 19:30
by amateur+
mikeyww wrote:You could use a function. Adjust as needed.
Your function is useful and I'll use such ones for different methods (sendmessage, sendpoll, etc.). Thanks!
Although my question was about putting some data (question, options, text, etc.) to Send() parameters. But now I understood how to do it:

Code: Select all

 SomeVariable = nheihenh noeinen  8797
 whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
 myurl1 := "https://api.telegram.org/bot<here is bot token>/sendPoll"
whr.Open("POST", myurl1, true)
whr.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
data = chat_id=-<here is chat id>&question=Will we live on Mars?`%0A(%SomeVariable%)&options=["Yeah!","Nope","Blah blah blah"]&is_anonymous=false
whr.Send(data)
whr.WaitForResponse()
MsgBox % whr.ResponseText
My mistake earlier was putting ? sign before chat_id in data assignment.
Or even better (as Lexikos says WinHttpRequest does not use TLS 1.1 or 1.2 by default on Windows 7 and older):

Code: Select all

SomeVariable = nheihenh noeinen  8797
req := ComObjCreate("Msxml2.XMLHTTP")
myurl1 := "https://api.telegram.org/bot<here is bot token>/sendPoll"
req.open("POST", myurl1, true)
req.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
data = chat_id=-<here is chat id>&question=Will we live on Mars?`%0A(%SomeVariable%)&options=["Yeah!","Nope","Blah blah blah"]&is_anonymous=false
req.send(data)
while req.readyState != 4
    sleep 100
MsgBox % req.responseText

Re: POST HTTP method for Telegram bot

Posted: 27 Oct 2021, 20:55
by mikeyww
I see. Thank you for clarifying.

Re: POST HTTP method for Telegram bot

Posted: 27 Oct 2021, 22:31
by amateur+
mikeyww, I've adjusted your function via adding method as one of parameters: TelegramSend(pReq, pMethod := "sendMessage", pData := ""). So we can use one function for any of methods (sendPoll, sendMessage, pinMessage, unpinMessage, deleteMessage, etc etc). There will be line pReq.open("POST", Myurl pMethod, true) inside. It was a good idea with creating this kind of functions, it is very convinient now, thank you again!

Re: POST HTTP method for Telegram bot

Posted: 28 Oct 2021, 05:45
by mikeyww
Good to hear. Thank you for the update! Perhaps this will help others who would like a similar functionality.