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
Projects
B.I.T.S. 4.0
bits-companion
Commits
fa3dc529
Verified
Commit
fa3dc529
authored
Oct 19, 2021
by
Mroik
Browse files
Added logging and mqtt auth
parent
9548f751
Changes
3
Hide whitespace changes
Inline
Side-by-side
main.py
View file @
fa3dc529
...
...
@@ -3,12 +3,14 @@ import mt
from
config
import
(
SERVER
,
PORT
,
USERNAME
,
PASSWORD
,
)
def
main
():
mt
.
bot
=
Bot
(
SERVER
,
PORT
)
mt
.
bot
.
start
()
mt
.
bot
.
start
(
USERNAME
,
PASSWORD
)
if
__name__
==
"__main__"
:
...
...
mt/bot.py
View file @
fa3dc529
...
...
@@ -13,7 +13,8 @@ class Bot:
self
.
mqtt
.
on_connect
=
on_connect
self
.
mqtt
.
on_message
=
on_message
def
start
(
self
):
def
start
(
self
,
username
,
password
):
self
.
mqtt
.
username_pw_set
(
username
,
password
)
self
.
mqtt
.
connect
(
self
.
server
,
self
.
port
)
self
.
updater
.
start_polling
()
try
:
...
...
tgbot/handlers.py
View file @
fa3dc529
import
logging
from
telegram.ext
import
CallbackContext
,
DispatcherHandlerStop
from
telegram
import
Update
from
telegram
import
Update
,
Message
import
mt
from
config
import
ADMIN_CHANNELS
LOGGER
=
logging
.
getLogger
(
__name__
)
def
on_message
(
update
:
Update
,
context
:
CallbackContext
):
from
.
import
where_am_i
# Because circular imports
print
(
"Message received"
)
if
update
.
message
.
chat
.
id
not
in
where_am_i
:
where_am_i
.
append
(
update
.
message
.
chat
.
id
)
msg
:
Message
=
update
.
message
LOGGER
.
info
(
"Message received from {} by {}"
.
format
(
msg
.
chat
.
id
,
msg
.
from_user
.
id
))
if
msg
.
chat
.
id
not
in
where_am_i
:
where_am_i
.
append
(
msg
.
chat
.
id
)
def
check_admin
(
update
:
Update
,
context
:
CallbackContext
):
...
...
@@ -27,10 +33,12 @@ def check_admin(update: Update, context: CallbackContext):
def
set_open
(
update
:
Update
,
context
:
CallbackContext
):
if
not
check_admin
(
update
,
context
):
raise
DispatcherHandlerStop
mt
.
bot
.
mqtt
.
publish
(
"sede/status"
,
1
)
resp
=
mt
.
bot
.
mqtt
.
publish
(
"sede/status"
,
1
)
LOGGER
.
info
(
resp
.
mid
)
def
set_closed
(
update
:
Update
,
context
:
CallbackContext
):
if
not
check_admin
(
update
,
context
):
raise
DispatcherHandlerStop
mt
.
bot
.
mqtt
.
publish
(
"sede/status"
,
0
)
resp
=
mt
.
bot
.
mqtt
.
publish
(
"sede/status"
,
0
)
LOGGER
.
info
(
resp
.
mid
)
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