Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
pitte
bits-companion
Commits
7dc161a9
Verified
Commit
7dc161a9
authored
Oct 27, 2021
by
Mroik
Browse files
Added statefulness to the bot
parent
a30915c0
Changes
2
Hide whitespace changes
Inline
Side-by-side
mt/handlers.py
View file @
7dc161a9
...
...
@@ -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
}
\t
With:
{
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
):
...
...
tgbot/handlers.py
View file @
7dc161a9
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment