Ok, then I can save the introductory stuff how to set up a bot and get a bot ID, and chat ID
Now, here is a stripped down version of this script:
https://autohotkey.com/boards/viewtopic ... am#p192355
Just add your bot ID, chat ID and include
Coco's JSON library for easier parsing of the bot's responses. Add name and path of a textfile to save the messages (if you don't, there are some msgboxes, too, which can be used to check):
Code: Select all
#include json.ahk ; Coco's JSON library, get it here: https://autohotkey.com/boards/viewtopic.php?t=627
;--------------------------------------------------------------------------------------------------
botToken := "xxxxxxxxx:yyyyyyyyyyyyyyyyyyy" ; add your Telegram bot token
chatID := 000000001 ; add your chat ID
textfile := "" ; add file (path and) name to save the Telegram messages
;--------------------------------------------------------------------------------------------------
oCustomers := {} ; create Object for user ids who are allowed to send messages to your
oCustomers[chatID] := "My Name" ; add your chat id (and name if you want) to the customer object for testing purposes
offset := "" ; Telegram message offset
; Check for new updates
SetTimer, UpdateTimer, 15000 ; set to 1000 ms = 1 second or similar, if you want (first comment the msgboxes out and a textfile path)
return
;---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Esc::ExitApp ; hit Escape to stop the script
;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UpdateTimer: ; checks constantly for user input in your bot
stack := {} ; message stack
updates := GetUpdates(botToken, (offset+1)) ; get (new) updates from your bot as JSON string; keep track of old messages
msgbox % "JSON response:`n" updates ; remove, if you use a textfile
try oUpdates := JSON.Load(updates) ; create an AHK object from the JSON string
If oUpdates.ok ; check if json answer was "ok" : true
{
loop % oUpdates.result.MaxIndex() ; determine number of new messages (updates)
stack.Push(oUpdates.result[A_index]) ; add all updates (=messages) to stack
For key, msg in stack
{
from_id := first_name := mtext := last_name := username := ""
from_id := msg.message.from.id ; which ID sent the message?
mtext := msg.message.text ; what was the message text?
;first_name := msg.message.from.first_name
;last_name := msg.message.from.last_name
;username ;= msg.message.from.username
msgbox % "userId: " from_id "`n" mtext ; remove when you add a textfile to collect the messages
offset := msg.update_id ; keep track of processed messages -> gets updated on Telegram server only with next call of GetUpdates(...)
if (textfile != "") ; checks, if there is a textfile to save to
if oCustomers.Haskey(from_id) ; check for known users... optional
FileAppend, %mtext% `n`n, %textfile% ; appends message to textfile and adds two linefeeds
}
}
return
;------------------------------------------ Telegram functions --------------------------------------------------------------------------------------------------------
GetUpdates(token, 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" token "/getupdates?offset=" offset "&limit=" updlimit "&timeout=" timeout
updjson := URLDownloadToVar(url)
return updjson
}
;----------------------------------- additional functions ------------------------------------------------------------------------------------------------------------------
URLDownloadToVar(url,ByRef variable=""){ ; function originally by Maestrith, I think
try ; keep script from breaking if API is down or not reacting
{
hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
hObject.Open("GET",url)
hObject.Send()
variable:=hObject.ResponseText
return variable
}
}
The msgboxes are mainly for testing/debugging, or in case you haven't added a path to a textfile yet, that collects all the text messages sent to the bot. They can be removed, if it works (and then the update frequency of the timer can be increased, if you like).
Since anybody can enter a private chat with your bot and send messages, I added a check so that only the messages of known chat IDs are added to the text file. That's why you will have to add your own chat ID (the msgboxes will show it for checking); of course, you can add your co-workers's/wife's/second phone's/whoever's chat ID as well, or remove this check completely.
If this script is not running on your computer while you send a message from your phone, it will still get all messages from the last 24 hours, when you start it next time. Older messages will be discarded by the API.
But there is also a webapp and a desktop app that can be used on Windows to copy the messages by hand later, if necessary (unfortunately, Google Speech is not accessible from these Windows apps).
Of course, you can change how the messages are processed by AHK, for example, if you want to save them separately. Let me know, if something is unclear.
Btw, Google speech can be started with the key just left to the space key on the Telegram keyboard; you might have to hold it for a short time until a small popup appears. Choose the microphone icon there and it will be remembered as the default action (at least, for some time). At least, it was like this on the Android phones I have seen...