Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

telegram.py 2.9KB

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