Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AddonInfo <- {
- "Name":"Telegram bot"
- "Summary":"Telegram bot API"
- "Version":"0.01"
- "Author":"NovaSquirrel"
- };
- // Calls a Telegram API function and calls a callback
- function DoAPI(Method, Parameters, Function, Extra) {
- function DoNothing(C, D, E){};
- const Key = "insert key here";
- local URL = "https://api.telegram.org/bot"+Key+"/"+Method;
- if(Function == null)
- Function = DoNothing;
- if(Parameters)
- api.CurlPost(Function, Extra, URL, api.MakeForms(Parameters));
- else
- api.CurlGet(Function, Extra, URL);
- }
- // Telegram sends chat IDs as big integers, but Squirrel by default only has 32-bit ones
- // so change the integers to the strings they should have been in the first place
- function ChatIDFix(Text) {
- function isdigit(character) {
- return character >= '0' && character <= '9';
- }
- local FindMe = @"""chat"":{""id"":";
- local FixedText = "";
- local CurIndex = 0;
- while(1) {
- local FindIndex = Text.find(FindMe, CurIndex);
- if(FindIndex == null) {
- FixedText += Text.slice(CurIndex);
- break;
- }
- // add the text up until the found thing
- FixedText += Text.slice(CurIndex, FindIndex)+FindMe+"\"";
- CurIndex = FindIndex+FindMe.len();
- // add the number, and close off the quotation mark
- while(isdigit(Text[CurIndex]))
- FixedText += format("%c", Text[CurIndex++]);
- FixedText += "\"";
- }
- return FixedText;
- }
- local UpdateId = 0;
- function ApiTestCmd(T, P, C) {
- P = api.CmdParamSplit(P);
- function RequestUpdates() {
- if(UpdateId)
- DoAPI("getUpdates", {"offset":UpdateId+1, "timeout":60}, GotAnswer, null);
- else
- DoAPI("getUpdates", {"timeout":60}, GotAnswer, null);
- }
- // Got an update
- function GotUpdate(Code, Data, Extra) {
- Data = api.DecodeJSON(ChatIDFix(Data));
- foreach(Update in Data.result) {
- if(Update.update_id > UpdateId)
- UpdateId = Update.update_id;
- local Message = Update.message;
- local From = ("from" in Message)?Message.from.username:"?";
- local Text = ("text" in Message)?Message.text:null;
- if(Text) {
- print(From+":"+Text);
- DoAPI("sendMessage", {"chat_id":Message.chat.id, "text":Text}, null, null);
- }
- }
- RequestUpdates();
- }
- RequestUpdates();
- return EventReturn.HANDLED;
- }
- api.AddCommandHook("tgtest", ApiTestCmd, Priority.NORMAL, null, null);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement