Advertisement
NovaYoshi

dumb telegram bot

Sep 2nd, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. AddonInfo <- {
  2. "Name":"Telegram bot"
  3. "Summary":"Telegram bot API"
  4. "Version":"0.01"
  5. "Author":"NovaSquirrel"
  6. };
  7.  
  8. // Calls a Telegram API function and calls a callback
  9. function DoAPI(Method, Parameters, Function, Extra) {
  10. function DoNothing(C, D, E){};
  11. const Key = "secret";
  12. local URL = "https://api.telegram.org/bot"+Key+"/"+Method;
  13.  
  14. if(Function == null)
  15. Function = DoNothing;
  16. if(Parameters)
  17. api.CurlPost(Function, Extra, URL, api.MakeForms(Parameters));
  18. else
  19. api.CurlGet(Function, Extra, URL);
  20. }
  21.  
  22. // Telegram sends chat IDs as big integers, but Squirrel by default only has 32-bit ones
  23. // so change the integers to the strings they should have been in the first place
  24. function ChatIDFix(Text) {
  25. function isdigit(character) {
  26. return (character >= '0' && character <= '9') || character == '-';
  27. }
  28.  
  29. local FindMe = @"""chat"":{""id"":";
  30. local FixedText = "";
  31. local CurIndex = 0;
  32.  
  33. while(1) {
  34. local FindIndex = Text.find(FindMe, CurIndex);
  35. if(FindIndex == null) {
  36. FixedText += Text.slice(CurIndex);
  37. break;
  38. }
  39. // add the text up until the found thing
  40. FixedText += Text.slice(CurIndex, FindIndex)+FindMe+"\"";
  41. CurIndex = FindIndex+FindMe.len();
  42.  
  43. // add the number, and close off the quotation mark
  44. while(isdigit(Text[CurIndex]))
  45. FixedText += format("%c", Text[CurIndex++]);
  46. FixedText += "\"";
  47. }
  48.  
  49. return FixedText;
  50. }
  51.  
  52. local UpdateId = 0;
  53.  
  54. function StartBotCmd(T, P, C) {
  55. api.TempMessage("Telegram bot started", C);
  56.  
  57. function RequestUpdates() {
  58. if(UpdateId)
  59. DoAPI("getUpdates", {"offset":UpdateId+1, "timeout":60}, GotUpdate, null);
  60. else
  61. DoAPI("getUpdates", {"timeout":60}, GotUpdate, null);
  62. }
  63.  
  64. // Got an update
  65. function GotUpdate(Code, Data, Extra) {
  66. local OldData = Data;
  67. Data = api.DecodeJSON(ChatIDFix(Data));
  68. if(!Data) {
  69. api.TempMessage(OldData, C);
  70. }
  71.  
  72. if("result" in Data)
  73. foreach(Update in Data.result) {
  74. if(Update.update_id > UpdateId)
  75. UpdateId = Update.update_id;
  76. if(!("message" in Update))
  77. continue;
  78. local Message = Update.message;
  79. local ChatId = Message.chat.id;
  80.  
  81. // Get "from" data if available
  82. local From = "";
  83. local FirstName = "";
  84. local LastName = "";
  85. if(!("username" in Message.from))
  86. Message.from.username <- Message.from.first_name;
  87. if("from" in Message) {
  88. From = ("from" in Message)?Message.from.username:"";
  89. FirstName = ("first_name" in Message.from)?Message.from.first_name:"";
  90. LastName = ("last_name" in Message.from)?Message.from.last_name:"";
  91. if(!From.len()) // username is optional for some reason
  92. From = FirstName;
  93. }
  94. local FullName = FirstName;
  95. if(LastName.len())
  96. FullName += " "+LastName;
  97.  
  98. local Text = ("text" in Message)?Message.text:null;
  99. if(Text) {
  100. print(From+":"+Text);
  101. }
  102. if(Text && Text[0] == '/') {
  103. local WithoutPrefix = Text.slice(1);
  104. local Split = api.TextParamSplit(WithoutPrefix);
  105.  
  106. local CommandName = Split[0][0];
  107.  
  108. // take out bot name if it's there
  109. local FindAtSign = CommandName.find("@");
  110. if(FindAtSign)
  111. CommandName = CommandName.slice(0, FindAtSign);
  112.  
  113. local BotCmdParams = {"Nick":From, "FirstName":FirstName, "LastName":LastName, "FullName":FullName,
  114. "Cmd":CommandName, "Arg":"", "Say":"telegramsay", "SayArg":ChatId+" "};
  115.  
  116. if(Split[0][0] != WithoutPrefix)
  117. BotCmdParams.Arg = Split[1][1];
  118.  
  119. api.Event("bot command", BotCmdParams C);
  120. }
  121. }
  122. RequestUpdates();
  123. }
  124. RequestUpdates();
  125. return EventReturn.HANDLED;
  126. }
  127.  
  128. function LtGtAmp(Text) {
  129. local Out = "";
  130. for(local i=0; i<Text.len(); i++) {
  131. local c = Text[i];
  132. if(c == '<')
  133. Out += "&lt;";
  134. else if(c == '>')
  135. Out += "&gt;";
  136. else if(c == '&')
  137. Out += "&amp;";
  138. else
  139. Out += api.Chr(c);
  140. }
  141. return Out;
  142. }
  143. function SayCmd(T, P, C) {
  144. P = api.TextParamSplit(P);
  145. local Italic = false;
  146. local Text = P[1][1];
  147. local Options = {"chat_id":P[0][0], "text":Text, "disable_web_page_preview":true};
  148.  
  149. if(Text[0] == 0x1d) { // italic
  150. Text = LtGtAmp(Text.slice(1));
  151. Options.parse_mode <- "HTML";
  152. Options.text = "<i>"+Text+"</i>";
  153. }
  154. DoAPI("sendMessage", Options, null, null);
  155. return EventReturn.HANDLED;
  156. }
  157.  
  158. api.AddCommandHook("telegramsay", SayCmd, Priority.NORMAL, null, null);
  159. api.AddCommandHook("telegrambot", StartBotCmd, Priority.NORMAL, null, null);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement