98 lines
2.5 KiB
Python
98 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import sys
|
|
sys.path.append(os.getcwd())
|
|
|
|
from cx93001.cx93001 import CX93001
|
|
import telebot
|
|
import time
|
|
import argparse
|
|
|
|
def telegram_bot(key):
|
|
bot = telebot.TeleBot(
|
|
key, parse_mode=None
|
|
)
|
|
return bot
|
|
|
|
def allow_user(modem):
|
|
print("Accepting call...")
|
|
for i in range(3):
|
|
if modem.accept_call():
|
|
print(f"Call accepted on try #{i+1}")
|
|
break
|
|
time.sleep(0.4)
|
|
time.sleep(0.9)
|
|
for _ in range(3):
|
|
modem.play_tones('1#', duration=700)
|
|
time.sleep(0.8)
|
|
print(f"Hanging up")
|
|
modem.hang_up()
|
|
|
|
def reject_user(modem):
|
|
modem.reject_call()
|
|
|
|
def run_liftbot():
|
|
parser = argparse.ArgumentParser(description="Liftbot")
|
|
parser.add_argument(
|
|
"--key",
|
|
type=str,
|
|
required=True,
|
|
help="Telegram bot key",
|
|
)
|
|
parser.add_argument(
|
|
"--chat",
|
|
type=str,
|
|
required=True,
|
|
nargs="+",
|
|
help="Chat to alert when things happen",
|
|
)
|
|
parser.add_argument(
|
|
"--allowed-numbers",
|
|
type=str,
|
|
nargs="+",
|
|
help="Add a number to the list of valid numbers",
|
|
)
|
|
parser.add_argument(
|
|
"--modem",
|
|
type=str,
|
|
default="/dev/ttyACM0",
|
|
help="Port of the modem to use",
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
bot = telegram_bot(args.key)
|
|
valid_numbers = {
|
|
'66942627': 'Centro Lift',
|
|
}
|
|
for num in args.allowed_numbers:
|
|
fields = num.split("=", 2)
|
|
num = fields[0]
|
|
num_name =fields[0]
|
|
if len(fields) > 1:
|
|
num_name = "=".join(fields[1:])
|
|
valid_numbers[num] = num_name
|
|
chats = [args.chat]
|
|
print("Starting Liftbot...")
|
|
modem = CX93001(port=args.modem)
|
|
while True:
|
|
ringtime, cid = modem.wait_call(max_rings_ignore_cid=3)
|
|
print(f"Incoming call from {cid} @ {ringtime}")
|
|
if cid in valid_numbers:
|
|
print(f"Call from {valid_numbers[cid]}")
|
|
allow_user(modem)
|
|
for chat in chats:
|
|
bot.send_message(chat, f"Incoming call from {cid} for {valid_numbers[cid]} -- allowing them up")
|
|
print(f"Done with loop")
|
|
else:
|
|
print(f"Unrecognized number: {cid} -- rejecting")
|
|
allow_user(modem)
|
|
for chat in chats:
|
|
bot.send_message(chat, f"Unrecognized call from {cid} -- allowing them for now, until we figure out the bug")
|
|
#reject_user(modem)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run_liftbot()
|