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 = "secret";
- 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') || character == '-';
- }
- 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 StartBotCmd(T, P, C) {
- api.TempMessage("Telegram bot started", C);
- function RequestUpdates() {
- if(UpdateId)
- DoAPI("getUpdates", {"offset":UpdateId+1, "timeout":60}, GotUpdate, null);
- else
- DoAPI("getUpdates", {"timeout":60}, GotUpdate, null);
- }
- // Got an update
- function GotUpdate(Code, Data, Extra) {
- local OldData = Data;
- Data = api.DecodeJSON(ChatIDFix(Data));
- if(!Data) {
- api.TempMessage(OldData, C);
- }
- if("result" in Data)
- foreach(Update in Data.result) {
- if(Update.update_id > UpdateId)
- UpdateId = Update.update_id;
- if(!("message" in Update))
- continue;
- local Message = Update.message;
- local ChatId = Message.chat.id;
- // Get "from" data if available
- local From = "";
- local FirstName = "";
- local LastName = "";
- if(!("username" in Message.from))
- Message.from.username <- Message.from.first_name;
- if("from" in Message) {
- From = ("from" in Message)?Message.from.username:"";
- FirstName = ("first_name" in Message.from)?Message.from.first_name:"";
- LastName = ("last_name" in Message.from)?Message.from.last_name:"";
- if(!From.len()) // username is optional for some reason
- From = FirstName;
- }
- local FullName = FirstName;
- if(LastName.len())
- FullName += " "+LastName;
- local Text = ("text" in Message)?Message.text:null;
- if(Text) {
- print(From+":"+Text);
- }
- if(Text && Text[0] == '/') {
- local WithoutPrefix = Text.slice(1);
- local Split = api.TextParamSplit(WithoutPrefix);
- local CommandName = Split[0][0];
- // take out bot name if it's there
- local FindAtSign = CommandName.find("@");
- if(FindAtSign)
- CommandName = CommandName.slice(0, FindAtSign);
- local BotCmdParams = {"Nick":From, "FirstName":FirstName, "LastName":LastName, "FullName":FullName,
- "Cmd":CommandName, "Arg":"", "Say":"telegramsay", "SayArg":ChatId+" "};
- if(Split[0][0] != WithoutPrefix)
- BotCmdParams.Arg = Split[1][1];
- api.Event("bot command", BotCmdParams C);
- }
- }
- RequestUpdates();
- }
- RequestUpdates();
- return EventReturn.HANDLED;
- }
- function LtGtAmp(Text) {
- local Out = "";
- for(local i=0; i<Text.len(); i++) {
- local c = Text[i];
- if(c == '<')
- Out += "<";
- else if(c == '>')
- Out += ">";
- else if(c == '&')
- Out += "&";
- else
- Out += api.Chr(c);
- }
- return Out;
- }
- function SayCmd(T, P, C) {
- P = api.TextParamSplit(P);
- local Italic = false;
- local Text = P[1][1];
- local Options = {"chat_id":P[0][0], "text":Text, "disable_web_page_preview":true};
- if(Text[0] == 0x1d) { // italic
- Text = LtGtAmp(Text.slice(1));
- Options.parse_mode <- "HTML";
- Options.text = "<i>"+Text+"</i>";
- }
- DoAPI("sendMessage", Options, null, null);
- return EventReturn.HANDLED;
- }
- api.AddCommandHook("telegramsay", SayCmd, Priority.NORMAL, null, null);
- api.AddCommandHook("telegrambot", StartBotCmd, Priority.NORMAL, null, null);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement