|
@@ -0,0 +1,92 @@
|
|
1
|
+#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+# ----------------------------------------------------------------------------
|
|
4
|
+# "THE BEER-WARE LICENSE" (Revision 42):
|
|
5
|
+# <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
|
|
6
|
+# you can do whatever you want with this stuff. If we meet some day, and you
|
|
7
|
+# think this stuff is worth it, you can buy me a beer in return. Thomas Buck
|
|
8
|
+# ----------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+import util
|
|
11
|
+import time
|
|
12
|
+import sys
|
|
13
|
+import json
|
|
14
|
+from config import Config
|
|
15
|
+
|
|
16
|
+class TelegramBot:
|
|
17
|
+ def __init__(self):
|
|
18
|
+ if (Config.telegram_key == None) or len(Config.telegram_key) < 10:
|
|
19
|
+ raise RuntimeError("No Telegram Bot API Key set!")
|
|
20
|
+
|
|
21
|
+ self.get = None
|
|
22
|
+ self.post = None
|
|
23
|
+ self.previous_offset = None
|
|
24
|
+ self.api = "https://api.telegram.org/bot" + Config.telegram_key + "/"
|
|
25
|
+
|
|
26
|
+ def message(self, text):
|
|
27
|
+ for chat in Config.telegram_whitelist:
|
|
28
|
+ data = {
|
|
29
|
+ "chat_id": chat,
|
|
30
|
+ "text": text,
|
|
31
|
+ }
|
|
32
|
+ r = self.fetch(self.api + "sendMessage", data)
|
|
33
|
+ print(r)
|
|
34
|
+
|
|
35
|
+ def poll(self):
|
|
36
|
+ if self.previous_offset != None:
|
|
37
|
+ data = {
|
|
38
|
+ "offset": self.previous_offset + 1,
|
|
39
|
+ }
|
|
40
|
+ r = self.fetch(self.api + "getUpdates", data)
|
|
41
|
+ else:
|
|
42
|
+ r = self.fetch(self.api + "getUpdates")
|
|
43
|
+
|
|
44
|
+ print(r)
|
|
45
|
+
|
|
46
|
+ for msg in r["result"]:
|
|
47
|
+ if (self.previous_offset == None) or (self.previous_offset < msg["update_id"]):
|
|
48
|
+ self.previous_offset = msg["update_id"]
|
|
49
|
+
|
|
50
|
+ def fetch(self, url, data = None):
|
|
51
|
+ # lazily initialize WiFi
|
|
52
|
+ if self.get == None:
|
|
53
|
+ self.get, self.post = util.getRequests()
|
|
54
|
+ if self.get == None:
|
|
55
|
+ return None
|
|
56
|
+
|
|
57
|
+ try:
|
|
58
|
+ if data == None:
|
|
59
|
+ print("GET " + url)
|
|
60
|
+ r = self.get(url)
|
|
61
|
+ else:
|
|
62
|
+ js = json.dumps(data)
|
|
63
|
+ print("POST " + url + " " + js)
|
|
64
|
+ r = self.post(url, headers = { "Content-Type": "application/json" }, data = js)
|
|
65
|
+
|
|
66
|
+ # explitic close on Response object not needed,
|
|
67
|
+ # handled internally by r.content / r.text / r.json()
|
|
68
|
+ # to avoid this automatic behaviour, first access r.content
|
|
69
|
+ # to trigger caching it in response object, then close
|
|
70
|
+ # socket.
|
|
71
|
+ tmp = r.content
|
|
72
|
+ if hasattr(r, "raw"):
|
|
73
|
+ if r.raw != None:
|
|
74
|
+ r.raw.close()
|
|
75
|
+ r.raw = None
|
|
76
|
+
|
|
77
|
+ return r.json()
|
|
78
|
+ except Exception as e:
|
|
79
|
+ print()
|
|
80
|
+ print(url)
|
|
81
|
+ if hasattr(sys, "print_exception"):
|
|
82
|
+ sys.print_exception(e)
|
|
83
|
+ else:
|
|
84
|
+ print(e)
|
|
85
|
+ print()
|
|
86
|
+ return None
|
|
87
|
+
|
|
88
|
+if True:#__name__ == "__main__":
|
|
89
|
+ b = TelegramBot()
|
|
90
|
+ b.message("Hello World!")
|
|
91
|
+ while True:
|
|
92
|
+ b.poll()
|