POST HTTP method for Telegram bot Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

POST HTTP method for Telegram bot

25 Oct 2021, 23:07

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.
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: POST HTTP method for Telegram bot

26 Oct 2021, 04:59

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.
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: POST HTTP method for Telegram bot

27 Oct 2021, 02:22

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).
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: POST HTTP method for Telegram bot  Topic is solved

27 Oct 2021, 04:43

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
}
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: POST HTTP method for Telegram bot

27 Oct 2021, 19:30

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
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: POST HTTP method for Telegram bot

27 Oct 2021, 20:55

I see. Thank you for clarifying.
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: POST HTTP method for Telegram bot

27 Oct 2021, 22:31

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!
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: POST HTTP method for Telegram bot

28 Oct 2021, 05:45

Good to hear. Thank you for the update! Perhaps this will help others who would like a similar functionality.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Nerafius, RandomBoy and 192 guests