-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path__init__.py
More file actions
65 lines (56 loc) · 2.34 KB
/
__init__.py
File metadata and controls
65 lines (56 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from modules import app, cbpi
from thread import start_new_thread
import logging
import time
import requests
telegram_bot_token = None
telegram_chat_id = None
telegram = None
def telegramBotToken():
global telegram_bot_token
telegram_bot_token = cbpi.get_config_parameter("telegram_bot_token", None)
if telegram_bot_token is None:
print("INIT Telegram Bot Token")
try:
cbpi.add_config_parameter(
"telegram_bot_token", "", "text", "Telegram Bot Token")
except:
cbpi.notify("Telegram Error", "Unable to update database. Update CraftBeerPi and reboot.",
type="danger", timeout=None)
def telegramChatId():
global telegram_chat_id
telegram_chat_id = cbpi.get_config_parameter("telegram_chat_id", None)
if telegram_chat_id is None:
print("INIT Telegram Chat ID")
try:
cbpi.add_config_parameter(
"telegram_chat_id", "", "text", "Telegram Chat ID")
except:
cbpi.notify("Telegram Error", "Unable to update database. Update CraftBeerPi and reboot.",
type="danger", timeout=None)
@cbpi.initalizer(order=9000)
def init(cbpi):
global telegram
cbpi.app.logger.info("INITIALIZE Telegram PLUGIN")
telegramBotToken()
telegramChatId()
if telegram_bot_token is None or not telegram_bot_token:
cbpi.notify("Telegram Error", "Check that Telegram Bot Token is set",
type="danger", timeout=None)
elif telegram_chat_id is None or not telegram_chat_id:
cbpi.notify("Telegram Error", "Check that Telegram Chat ID is set",
type="danger", timeout=None)
else:
telegram = "OK"
@cbpi.event("MESSAGE", async=True)
def messageEvent(message):
if telegram_bot_token is not None and telegram_chat_id is not None:
text = "<b>" + message["headline"] + \
"</b>\n<i>" + message["message"] + "</i>"
url = "https://api.telegram.org/bot" + telegram_bot_token + "/sendMessage"
escapedUrl = requests.Request('GET', url,
params={"chat_id": telegram_chat_id,
"text": text,
"parse_mode": "HTML"},
).prepare().url
requests.get(escapedUrl)