Advertisement
sopyanx

tg_getUpdates

Mar 24th, 2019
2,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. :global TGLASTMSGID
  2. :global TGLASTUPDID
  3.  
  4. :local fconfig [:parse [/system script get tg_config source]]
  5. :local http [:parse [/system script get func_fetch source]]
  6. :local gkey [:parse [/system script get tg_getkey source]]
  7. :local send [:parse [/system script get tg_sendMessage source]]
  8.  
  9. :local cfg [$fconfig]
  10. :local trusted [:toarray ($cfg->"trusted")]
  11. :local botID ($cfg->"botAPI")
  12. :local storage ($cfg->"storage")
  13. :local timeout ($cfg->"timeout")
  14.  
  15. :put "cfg=$cfg"
  16. :put "trusted=$trusted"
  17. :put "botID=$botID"
  18. :put "storage=$storage"
  19. :put "timeout=$timeout"
  20.  
  21. :local file ($storage."tg_get_updates.txt")
  22. :local logfile ($storage."tg_fetch_log.txt")
  23. #get 1 message per time
  24. :local url ("https://api.telegram.org/bot".$botID."/getUpdates?timeout=$timeout&limit=1")
  25. :if ([:len $TGLASTUPDID]>0) do={
  26. :set url "$url&offset=$($TGLASTUPDID+1)"
  27. }
  28.  
  29. :put "Reading updates..."
  30. :local res [$http dst-path=$file url=$url resfile=$logfile]
  31. :if ($res!="success") do={
  32. :put "Error getting updates"
  33. return "Failed get updates"
  34. }
  35. :put "Finished to read updates."
  36.  
  37. :local content [/file get [/file find name=$file] contents]
  38.  
  39. :local msgid [$gkey key="message_id" text=$content]
  40. :if ($msgid="") do={
  41. :put "No new updates"
  42. :return 0
  43. }
  44. :set TGLASTMSGID $msgid
  45.  
  46. :local updid [$gkey key="update_id" text=$content]
  47. :set TGLASTUPDID $updid
  48.  
  49. :local fromid [$gkey block="from" key="id" text=$content]
  50. :local username [$gkey block="from" key="username" text=$content]
  51. :local firstname [$gkey block="from" key="first_name" text=$content]
  52. :local lastname [$gkey block="from" key="last_name" text=$content]
  53. :local chatid [$gkey block="chat" key="id" text=$content]
  54. :local chattext [$gkey block="chat" key="text" text=$content]
  55.  
  56. :put "message id=$msgid"
  57. :put "update id=$updid"
  58. :put "from id=$fromid"
  59. :put "first name=$firstname"
  60. :put "last name=$lastname"
  61. :put "username=$username"
  62. :local name "$firstname $lastname"
  63. :if ([:len $name]<2) do {
  64. :set name $username
  65. }
  66.  
  67. :put "in chat=$chatid"
  68. :put "command=$chattext"
  69.  
  70. :local allowed ( [:type [:find $trusted $fromid]]!="nil" or [:type [:find $trusted $chatid]]!="nil")
  71. :if (!$allowed) do={
  72. :put "Unknown sender, keep silence"
  73. :return -1
  74. }
  75.  
  76. :local cmd ""
  77. :local params ""
  78. :local ltext [:len $chattext]
  79.  
  80. :local pos [:find $chattext " "]
  81. :if ([:type $pos]="nil") do={
  82. :set cmd [:pick $chattext 1 $ltext]
  83. } else={
  84. :set cmd [:pick $chattext 1 $pos]
  85. :set params [:pick $chattext ($pos+1) $ltext]
  86. }
  87.  
  88. :local pos [:find $cmd "@"]
  89. :if ([:type $pos]!="nil") do={
  90. :set cmd [:pick $cmd 0 $pos]
  91. }
  92.  
  93. :put "cmd=<$cmd>"
  94. :put "params=<$params>"
  95.  
  96. :global TGLASTCMD $cmd
  97.  
  98. :put "Try to invoke external script tg_cmd_$cmd"
  99. :local script [:parse [/system script get "tg_cmd_$cmd" source]]
  100. $script params=$params chatid=$chatid from=$name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement