Verified Commit e494707b authored by Mroik's avatar Mroik
Browse files

Initial commit

No related merge requests found
Showing with 125 additions and 0 deletions
+125 -0
LICENSE 0 → 100644
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2021 Mroik <mroik@delayed.space>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
\ No newline at end of file
config.py 0 → 100644
from os import environ
SERVER = environ.get("BITS_SERVER")
PORT = int(environ.get("BITS_PORT", default=1883))
USERNAME = environ.get("BITS_USER")
PASSWORD = environ.get("BITS_PASSWORD", default="")
TG_KEY = environ.get("BITS_TG_KEY", default="")
main.py 0 → 100644
from mt.bot import Bot
from config import (
SERVER,
PORT,
)
def main():
bot = Bot(SERVER, PORT)
bot.start()
if __name__ == "__main__":
main()
File added
File added
File added
mt/bot.py 0 → 100644
from paho.mqtt.client import Client
from tgbot import updater
from mt.handlers import on_message, on_connect
class Bot:
def __init__(self, server, port=1883):
self.server = server
self.port = port
self.updater = updater
self.mqtt = Client()
def start(self):
self.mqtt.on_connect = on_connect
self.mqtt.on_message = on_message
self.mqtt.connect(self.server, self.port)
self.updater.start_polling()
print("test")
try:
self.mqtt.loop_forever()
except KeyboardInterrupt:
pass
self.updater.stop()
from tgbot import updater, where_am_i
def on_connect(client, userdata, flags, rc):
print("Connected with rc:", rc)
def on_message(client, userdata, msg):
print("Received:", msg.topic, "\nWith:", msg.payload.decode("utf-8"))
if msg.topic is not "sede/status":
return
if msg.payload.decode("utf-8") == 0:
for id_ in where_am_i:
updater.bot.send_message(id_, "Il POuL è aperto!")
elif msg.payload.decode("utf-8") == 1:
for id_ in where_am_i:
updater.bot.send_message(id_, "Il POuL è chiuso!")
def send_open(bot):
bot.mqtt.publish("sede/status", 1)
def send_closed(bot):
bot.mqtt.publish("sede/status", 0)
python-telegram-bot
paho-mqtt
\ No newline at end of file
from tgbot.dispatcher import setup
from config import TG_KEY
where_am_i = []
updater = setup(key=TG_KEY)
File added
File added
File added
from telegram.ext import Updater, MessageHandler, Filters
from tgbot import handlers
def setup(key):
updater = Updater(token=key)
dispatcher = updater.dispatcher
dispatcher.add_handler(MessageHandler(
filters=Filters.chat_type.groups,
callback=handlers.on_message
))
return updater
from telegram.ext import CallbackContext
from telegram import Update
def on_message(update: Update, context: CallbackContext):
from . import where_am_i # Because circular imports
if update.message.chat.id not in where_am_i:
where_am_i.append(update.message.chat.id)
def set_open(update: Update, context: CallbackContext):
"""TODO"""
pass
def set_closed(update: Update, context: CallbackContext):
"""TODO"""
pass
Supports Markdown
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