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

69 lines
2.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, CommandsContext, CommandsContextDelegate
from General import Text2Speech
from ..Control import Control
from .MyTeleBot import MyTeleBot
2021-08-21 02:09:37 +03:00
class TelegramBot(Control, CommandsContextDelegate):
2021-08-21 02:09:37 +03:00
threads = []
online = True
voids = 0
memory = []
voice = Text2Speech.Engine()
bot = MyTeleBot(config.telebot)
commandsContext: CommandsContext
# Control
2021-08-21 02:09:37 +03:00
def __init__(self):
self.commandsContext = CommandsContext(delegate = self)
def start(self):
while True:
try:
print("Start polling...")
self.bot.polling(callback = self.commandsContext.checkThreads)
except Exception as e:
print(e, "\nPolling failed")
time.sleep(10)
def stop(self):
raise NotImplementedError
def main(self, id, text):
self.commandsContext.processString(text.lower(), data = {'id': id})
# CommandsContextDelegate
def commandsContextDidReceiveResponse(self, response):
id = response.data.get('id')
if not id: return
2021-08-21 02:09:37 +03:00
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
# Telebot
2021-08-21 02:09:37 +03:00
@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):
self.commandsContext.processString(msg.text.lower(), data = {'id': msg.chat.id})