瀏覽代碼

add telegram test

Thomas Buck 9 月之前
父節點
當前提交
53f3de92e6
共有 2 個檔案被更改,包括 144 行新增0 行删除
  1. 52
    0
      games.py
  2. 92
    0
      telegram.py

+ 52
- 0
games.py 查看文件

@@ -0,0 +1,52 @@
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
+camp_pink = (251, 72, 196)
11
+camp_green = (63, 255, 33)
12
+
13
+from splash import SplashScreen
14
+from scroll import ScrollText
15
+from solid import Solid
16
+from life import GameOfLife
17
+from net import CheckHTTP
18
+from image import ImageScreen
19
+from qr import QRScreen
20
+from snake import Snake
21
+from gamepad import InputWrapper
22
+from manager import Manager
23
+from tetris import Tetris
24
+import util
25
+
26
+url_uba = "http://ubabot.frubar.net"
27
+url_printer = "http://i3-am8.fritz.box"
28
+
29
+scroll_speed = 50
30
+
31
+# Need to import InputWrapper before initializing RGB Matrix on Pi
32
+i = util.getInput()
33
+t = util.getTarget(i)
34
+
35
+# Loading fonts and graphics takes a while.
36
+# So show a splash screen while the user waits.
37
+splash = SplashScreen(t)
38
+t.loop_start()
39
+splash.draw()
40
+t.loop_end()
41
+
42
+# Main "Menu"
43
+m = Manager(t, i)
44
+m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), None, 2.0))
45
+m.add(Solid(t, 1.0))
46
+m.add(Tetris(t, i,))
47
+m.add(Solid(t, 1.0))
48
+m.add(Snake(t, i, camp_pink, camp_green))
49
+m.add(Solid(t, 1.0))
50
+
51
+m.restart()
52
+util.loop(t, m.draw)

+ 92
- 0
telegram.py 查看文件

@@ -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()

Loading…
取消
儲存