1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2025-02-17 11:55:35 +02:00
STARK/Controls/TelegramBot/TelegramBot.py

94 lines
3.1 KiB
Python
Raw Normal View History

2021-08-21 02:09:37 +03:00
#!/usr/local/bin/python3.8
2021-08-21 02:09:37 +03:00
import time
import os
import config
from ArchieCore import Command
from General import Text2Speech
from ..Control import Control
from .MyTeleBot import MyTeleBot
2021-08-21 02:09:37 +03:00
class TelegramBot(Control):
threads = []
online = True
voids = 0
memory = []
voice = Text2Speech.Engine()
bot = MyTeleBot(config.telebot)
2021-08-21 02:09:37 +03:00
def reply(self, id, response):
if response.text:
2021-09-12 17:31:16 +03:00
self.bot.send_message(id, response.text)
2021-08-21 02:09:37 +03:00
if response.voice:
2021-09-12 17:31:16 +03:00
if bytes := self.voice.generate(response.voice).getBytes():
self.bot.send_voice(id, bytes)
2021-08-21 02:09:37 +03:00
if response.thread: # add background thread to list
response.thread['id'] = id
2021-09-12 17:31:16 +03:00
self.threads.append(response.thread)
2021-08-21 02:09:37 +03:00
def check_threads(self, threads):
for thread in threads:
if thread['finish_event'].is_set():
response = thread['thread'].join()
2021-09-12 17:31:16 +03:00
self.reply(thread['id'], response)
2021-08-21 02:09:37 +03:00
thread['finish_event'].clear()
del thread
def main(self, id, text):
text = text.lower()
if Command.isRepeat(text):
2021-09-12 17:31:16 +03:00
self.reply(id, self.memory[0]['response']);
2021-08-21 02:09:37 +03:00
return
2021-09-12 17:31:16 +03:00
if self.memory:
response = self.memory[0]['response']
2021-08-21 02:09:37 +03:00
if response.callback:
if new_response := response.callback.answer(text):
2021-09-12 17:31:16 +03:00
self.reply(id, new_response)
2021-08-21 02:09:37 +03:00
memory.insert(0, {
'cmd': response.callback,
'params': None,
'response': new_response,
})
return
try:
2021-09-12 17:31:16 +03:00
cmd, params = self.memory[0]['cmd'].checkContext(text).values()
if self.memory[0].get('params'): params = {**memory[0].get('params'), **params}
2021-08-21 02:09:37 +03:00
except:
cmd, params = Command.reg_find(text).values()
response = cmd.start(params)
2021-09-12 17:31:16 +03:00
self.reply(id, response)
self.memory.insert(0, {
2021-08-21 02:09:37 +03:00
'cmd': cmd,
'params': params,
'response': response,
})
@bot.message_handler(commands=['vlc', 'queue', 'cmd'])
def simple_commands(msg):
command = msg.text.replace('/cmd', '').replace('/vlc', 'vlc')
if '/queue' in msg.text: command = command.replace('/queue', '') + '--playlist-enqueue'
os.system(f'lxterminal --command="{command}"')
@bot.message_handler(commands=['terminal'])
def terminal(msg):
command = msg.text.replace('/terminal', '')
output = os.popen(command).read()
bot.send_message(msg.chat.id, output)
@bot.message_handler(content_types = ['text'])
def execute(msg):
2021-09-12 17:31:16 +03:00
TelegramBot().main(msg.chat.id, msg.text)
2021-08-21 02:09:37 +03:00
def start(self):
while True:
try:
print("Start polling...")
2021-09-12 17:31:16 +03:00
self.bot.polling(callback = self.check_threads, args = (self.threads,) )
2021-09-12 15:33:46 +03:00
except Exception as e:
print(e, "\nPolling failed")
time.sleep(10)
2021-08-21 02:09:37 +03:00
if __name__ == '__main__':
TelegramBot().start()