doorbot/bot.py

57 lines
1.9 KiB
Python

from deltachat2 import MsgData, events
from deltabot_cli import BotCli
import RPi.GPIO as GPIO
import time
import subprocess
cli = BotCli("doorbot")
allowed_chats = [10,12,13]
@cli.on(events.RawEvent)
def log_event(bot, accid, event):
bot.logger.info(event)
def open_downstairs(bot, accid, msg):
pin = 24
if not msg.chat_id == cli.get_admin_chat(bot.rpc, accid):
bot.rpc.send_msg(accid, msg.chat_id, MsgData(text="permission denied"))
return
bot.rpc.send_reaction(accid, msg.id, ["🆗"])
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin,GPIO.HIGH)
time.sleep(3)
GPIO.output(pin,GPIO.LOW)
@cli.on(events.NewMessage)
def echo(bot, accid, event):
# TODO: check the time this was sent that it is not too far ago
# or and use realtime channels
msg = event.msg
if msg.text == "/open2":
if not msg.chat_id == cli.get_admin_chat(bot.rpc, accid):
bot.rpc.send_msg(accid, msg.chat_id, MsgData(text="permission denied"))
return
bot.rpc.send_reaction(accid, msg.id, ["🆗"])
try:
subprocess.run(["./unlock.sh"], timeout=20)
except subprocess.TimeoutExpired:
bot.rpc.send_reaction(accid, msg.id, [""])
elif msg.text.startswith("/open"):
try:
n = int(msg.text.removeprefix("/open"))
except ValueError:
n = 0
time.sleep(n)
open_downstairs(bot, accid, msg)
elif msg.text == "/id":
bot.rpc.send_msg(accid, msg.chat_id, MsgData(text="the id of this chat is {id}, add this to the allowlist to allow opening the door from this group".format(id = msg.chat_id)))
elif msg.text == "/help":
bot.rpc.send_msg(accid, msg.chat_id, MsgData(text="/open [delay]\n/open2\n/id - get ID of the chat"))
if __name__ == "__main__":
cli.start()