However I followed some steps and it began to sort of work.
This script connects telegram and ahk. As I understand, commands can be sent from phone's telegram to ahk for performing some tasks.
I have been able to send a message by running this script to the telegram bot that I have created .
This is my doubt (yes I have entered token,chat id e.t.c and downloaded the json file.)
Code: Select all
#include json.ahk ; Coco's JSON library, get it here: https://autohotkey.com/boards/viewtopic.php?t=627
botToken := "MyToken" ; add your Telegram bot token
chatID := MyChatID ; add your chat ID for testing porposes
oCustomers := {} ; Object of user ids of registered customers -> add your customers, you can send them a personal registration link
oCustomers[CHAT ID] := "" ; add your chat id (and name) to the customer object for testing purposes
offset := "" ; Telegram message offset
cmds := { "Command1" : "somefunction" ; bundle commands (case insensitive) and corresponding script functions to call
, "Command2" : "somefunction"
, "Command3" : "somefunction"
, "Show inline buttons" : "inlinebuttons"
, "Remove keyboard" : "RemoveKeyb" }
; add custom keyboard for testing
keyb= ; json formatted string
(
{"keyboard":[ ["Command1", "Command2"], ["Show inline buttons", "Remove Keyboard"] ], "resize_keyboard" : true }
)
url := "https://api.telegram.org/bot" botToken "/sendMessage?text=Keyboard added&chat_id=" chatID "&reply_markup=" keyb
json_message := URLDownloadToVar(url)
; msgbox % json_message
; Check for new updates
SetTimer, UpdateTimer, 1000 ; every 1000 ms = 1 second
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 % updates
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 := ""
if (msg.callback_query.data!="") ; inline buttons have to be handled differently than normal messages and normal keyboards
{
from_id := msg.callback_query.from.id
mtext := msg.callback_query.data
}
else ; get message text or message from normal keyboard
{
from_id := msg.message.from.id ; which ID sent the message?
mtext := msg.message.text
;first_name := msg.message.from.first_name
;last_name := msg.message.from.last_name
;username ;= msg.message.from.username
}
offset := msg.update_id ; keep track of processed messages
if oCustomers.Haskey(from_id) ; check for known users... optional
{
if cmds.Haskey(mtext) ; is message a known command?
{
fun := cmds[mtext] ;look up which function to call for a specific command
%fun%(botToken, msg, from_id, mtext) ; call function
}
else
{
text := "Sorry... I don't know this command."
; url encoding for messages : %0A = newline (\n) %2b = plus sign (+) %0D for carriage return \r single Quote ' : %27 %20 = space
SendText(botToken, text, from_id)
}
}
else
Traytip, No, Unknown user, 3
} ; end 'for'
}
return
;-------------------------------------------------------- functions for user commands ---------------------------------------------------------------------------------------------------------
somefunction(botToken, msg, from_id, mtext)
{
if (msg.callback_query.id!="") ; After the user presses a callback button, Telegram clients will display a progress bar until you call answerCallbackQuery
{
cbQuery_id := msg.callback_query.id
; Notification and alert are optional
url := "https://api.telegram.org/bot" botToken "/answerCallbackQuery?text=Notification&callback_query_id=" cbQuery_id "&show_alert=true"
json_message := URLDownloadToVar(url)
;msgbox % json_message
}
text := "You chose " mtext
SendText(botToken, text, from_id) ; url encoding for messages : %0A = newline (\n) %2b = plus sign (+) %0D for carriage return \r single Quote ' : %27 %20 = space
return
}
;--------------------------------------------------------------------
; add inline buttons
inlinebuttons(botToken, msg, from_id, mtext="")
{
keyb= ; json string:
(
{"inline_keyboard":[ [{"text": "Command One" , "callback_data" : "Command1"}, {"text" : "Some button (Cmd3)", "callback_data" : "Command3"} ] ], "resize_keyboard" : true }
)
url := "https://api.telegram.org/bot" botToken "/sendMessage?text=Keyboard added&chat_id=" from_id "&reply_markup=" keyb
json_message := URLDownloadToVar(url) ; find this function at the next code box below
return json_message
}
;---------------------------------------------------------------------
; Remove custom keyboard
RemoveKeyb(botToken, msg, from_id, mtext="")
{
keyb=
(
{"remove_keyboard" : true }
)
url := "https://api.telegram.org/bot" botToken "/sendMessage?text=Keyboard removed&chat_id=" from_id "&reply_markup=" keyb
json_message := URLDownloadToVar(url)
;msgbox % json_message
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
}
;------------------------------------------------
SendText(token, text, from_id, replyMarkup="", parseMode="" )
{
url := "https://api.telegram.org/bot" token "/sendmessage?chat_id=" from_ID "&text=" text "&reply_markup=" replyMarkup "&parse_mode=" parseMode
json_message := URLDownloadToVar(url)
return json_message
}
;----------------------------------- 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
}
}
In line 14 it says cmds := { "Command1" : "somefunction" }
the script adds a keyboard to the bot chat (ref img)
This is a simple function
Code: Select all
myfunc()
return
myfunc(){
MsgBox HI
}
1. So will including the above function in { "Command1" : "somefunction" } Call it ?
2. How to call that function from the telegram api script ?
Kindly do not take the trouble of downloading and testing. My lack of coding knowledge will result in your time being wasted.
If this question is as simple as I think, please let me know and answer at your convenience
Thank you !
Regards.