Hi blue83, in general, the code should already be there in these two posts:
here and
here.
Which code are you currently using? It helps to know what we are actually talking about... and I might be able to spot problems in your code.
blue83 wrote: ↑25 May 2020, 03:32
For reading messagers I only get " {"ok":true,"result":[]}" as a result.
Can you post a code just for reading messages, because for sending there is no problem.
Well, that response looks promising. You seem to have a valid connection to your bot's messages.
This particular response usually means that there were no (new,
unconfirmed) messages from users.
To be clear, a user (for example,
you) needs to actually type in a message into the chat with the bot. It won't return the messages you sent via your bot to a user - or yourself (you should keep track of these yourself).
A very basic test script (that should show - also repeatedly - the same first 5 messages you got in the last 24 hours in a
msgbox; for testing, it doesn't use an offset to ignore "old" messages - we'll get to that later). Don't forget to add your bot token:
Code: Select all
; #include json.ahk ; include to process JSON strings as objects
token := "your bot token here"
q:: ; press q to get updates
update := GetUpdates(token, , 5) ; limit result to five messages
msgbox % update
; oUpd := JSON.Load(update) ; load JSON response string as an AHK object
return
Esc::ExitApp
GetUpdates(botToken, offset="", updlimit=100, timeout=0){
If (updlimit>100)
updlimit := 100
; Offset = Identifier of the first update to be returned.
url := "https://api.telegram.org/bot" botToken "/getupdates?offset=" offset "&limit=" updlimit "&timeout=" timeout
updjson := URLDownloadToVar(url)
return updjson
}
URLDownloadToVar(url,ByRef variable=""){
try
{
hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
hObject.Open("GET",url)
hObject.Send()
variable:=hObject.ResponseText
return variable
}
}
Type a few messages into your chat with the bot, and press
q to get updates.
blue83 wrote: ↑25 May 2020, 03:32
And maybe to read only new messages?
For this, you would use the
offset parameter:
gregster wrote: ↑29 Dec 2017, 07:54
The
offset parameter is useful to tell Telegram to not deliver the same messages over and over to you. The latest offset you can get from actually parsing the JSON response and then use it for your next Update call.
To get the correct (latest) offset you actually need to parse the messages - and use the most recent
update_id plus 1 in your next update call, iirc. With this, all messages with
update_ids lower than that offset are "confirmed" and won't show up anymore in future updates.
I have shown this some time ago, among other things, in the code in
this post. Did you try it?
Please note that you should also add your personal chat id to that code to make it work (because I restricted it to known users; of course, you can modify/remove this restriction). If you don't know your own chat ID, you can use the script above to determine it. Type a message and use the
from.id part from the json string in the msgbox. But if you were already sending messages, that's probably clear.
This code parses new messages for known strings like
Command1,
Command2 and and reacts/runs code based on these messages/commands. Of course, you can also process these messages in other ways...
Try to type
Command1 or use one of the cutom keyboard buttons... and the bot will give some (very simple
) feedback:
- telegrambot example.png (10.4 KiB) Viewed 3913 times
I hope this helps!