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 = Bot(SERVER, PORT), 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(f"On connect RC: {RETURN_CODE[rc]}")
res, mid = client.subscribe("sede/status") # Maybe should make it into a constant set'd from config file"{res} {mid}")
def on_subscribe(client, userdata, mid, granted_qos):"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":
if msg.payload.decode("utf-8") == "1":
if payload["status"] == OPEN:
for id_ in where_am_i:, "Il POuL è aperto!")
elif msg.payload.decode("utf-8") == "0":
elif payload["status"] == CLOSED:
for id_ in where_am_i:, "Il POuL è chiuso!")
def on_publish(client, userdata, mid):"{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 =
return True
if - timedelta(seconds=20) < last_change:
return True
return False
def set_open(update: Update, context: CallbackContext):
LOGGER.debug("Trying to open")
if check_debounce():
if not check_admin(update, context):
raise DispatcherHandlerStop
resp ="sede/status", 1)
resp ="sede/status", payload=json.dumps({"id": "Set from telegram", "status": OPEN}))
def set_closed(update: Update, context: CallbackContext):
global last_change
if last_change is None:
last_change =
if - 10000 < last_change:
LOGGER.debug("Trying to close")
if check_debounce():
if not check_admin(update, context):
raise DispatcherHandlerStop
resp ="sede/status", 0)
resp ="sede/status", payload=json.dumps({"id": "Set from telegram", "status": CLOSED}))
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