Verified Commit 7dc161a9 authored by Mroik's avatar Mroik
Browse files

Added statefulness to the bot

parent a30915c0
......@@ -6,6 +6,7 @@ from mt.const import RETURN_CODE, OPEN, CLOSED
LOGGER = logging.getLogger(__name__)
is_open = None
def on_connect(client, userdata, flags, rc):
......@@ -21,16 +22,19 @@ def on_subscribe(client, userdata, mid, granted_qos):
def on_message(client, userdata, msg):
global is_open
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 payload["status"] == OPEN:
if payload["status"] == OPEN and (is_open is None or not is_open):
for id_ in where_am_i:
updater.bot.send_message(id_, "🟢🟢 Il POuL è aperto! 🟢🟢")
elif payload["status"] == CLOSED:
is_open = True
elif payload["status"] == CLOSED and (is_open is None or is_open):
for id_ in where_am_i:
updater.bot.send_message(id_, "🔴🔴 Il POuL è chiuso! 🔴🔴")
is_open = False
def on_publish(client, userdata, mid):
......
......@@ -50,13 +50,13 @@ def check_debounce():
def set_open(update: Update, context: CallbackContext):
if is_open:
if is_open and is_open is not None:
return
set_status(True, update, context)
def set_closed(update: Update, context: CallbackContext):
if not is_open:
if not is_open and is_open is not None:
return
set_status(False, update, context)
......
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