Verified Commit 5c38884d authored by Mroik's avatar Mroik
Browse files

Fixed set open or closed from tg

parent 9da690a9
......@@ -13,7 +13,7 @@ LOGGER = logging.getLogger(__name__)
def main():
# logging.basicConfig(level=logging.INFO) # Uncomment for DUBUG
# logging.basicConfig(level=logging.DEBUG) # Uncomment for DUBUG
mt.bot = Bot(SERVER, PORT)
mt.bot.start(USERNAME, PASSWORD)
......
from paho.mqtt.client import Client
from tgbot import updater
from mt.handlers import on_message, on_connect
from mt.handlers import on_message, on_connect, on_subscribe, on_publish
class Bot:
......@@ -11,6 +11,8 @@ class Bot:
self.updater = updater
self.mqtt = Client(client_id="Telegram_notifier")
self.mqtt.on_connect = on_connect
self.mqtt.on_publish = on_publish
self.mqtt.on_subscribe = on_subscribe
self.mqtt.on_message = on_message
def start(self, username, password):
......
......@@ -6,3 +6,5 @@ RETURN_CODE = (
"Connection refused – bad username or password",
"Connection refused – not authorised"
)
OPEN = "open"
CLOSED = "closed"
import logging
import json
from tgbot import updater, where_am_i
from mt.const import RETURN_CODE
from mt.const import RETURN_CODE, OPEN, CLOSED
LOGGER = logging.getLogger(__name__)
......@@ -9,19 +10,28 @@ LOGGER = logging.getLogger(__name__)
def on_connect(client, userdata, flags, rc):
if rc > 0:
LOGGER.error(RETURN_CODE[rc])
LOGGER.error(f"On connect RC: {RETURN_CODE[rc]}")
return
res, mid = client.subscribe("sede/status") # Maybe should make it into a constant set'd from config file
LOGGER.info(f"{res} {mid}")
def on_subscribe(client, userdata, mid, granted_qos):
LOGGER.info(f"Sub RC: {mid}")
def on_message(client, userdata, msg):
LOGGER.debug("Received:", msg.topic, "\tWith:", msg.payload.decode("utf-8"))
LOGGER.debug(f"Received: {msg.topic}\tWith: {msg.payload.decode('utf-8')}")
payload = json.loads(msg.payload.decode("utf-8"))
if msg.topic != "sede/status":
return
if msg.payload.decode("utf-8") == "1":
if payload["status"] == OPEN:
for id_ in where_am_i:
updater.bot.send_message(id_, "Il POuL è aperto!")
elif msg.payload.decode("utf-8") == "0":
elif payload["status"] == CLOSED:
for id_ in where_am_i:
updater.bot.send_message(id_, "Il POuL è chiuso!")
def on_publish(client, userdata, mid):
LOGGER.info(f"{userdata} {mid}")
import logging
from datetime import datetime
import json
from datetime import datetime, timedelta
from telegram.ext import CallbackContext, DispatcherHandlerStop
from telegram import Update, Message
import mt
from mt.const import OPEN, CLOSED
from config import ADMIN_CHANNELS
......@@ -32,24 +34,34 @@ def check_admin(update: Update, context: CallbackContext):
return is_admin
def check_debounce():
global last_change
if last_change is None:
last_change = datetime.now()
return True
else:
if datetime.now() - timedelta(seconds=20) < last_change:
return True
return False
def set_open(update: Update, context: CallbackContext):
LOGGER.debug("Trying to open")
if check_debounce():
return
if not check_admin(update, context):
raise DispatcherHandlerStop
resp = mt.bot.mqtt.publish("sede/status", 1)
resp = mt.bot.mqtt.publish("sede/status", payload=json.dumps({"id": "Set from telegram", "status": OPEN}))
LOGGER.info(resp.mid)
def set_closed(update: Update, context: CallbackContext):
global last_change
if last_change is None:
last_change = datetime.now()
else:
if datetime.now() - 10000 < last_change:
print(datetime.now())
return
LOGGER.debug("Trying to close")
if check_debounce():
return
if not check_admin(update, context):
raise DispatcherHandlerStop
resp = mt.bot.mqtt.publish("sede/status", 0)
resp = mt.bot.mqtt.publish("sede/status", payload=json.dumps({"id": "Set from telegram", "status": CLOSED}))
LOGGER.info(resp.mid)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment