2021-08-21 02:09:37 +03:00
|
|
|
#!/usr/local/bin/python3.8
|
2021-08-22 17:24:30 +03:00
|
|
|
|
2021-08-21 02:09:37 +03:00
|
|
|
import time
|
|
|
|
import os
|
2021-08-22 17:24:30 +03:00
|
|
|
import config
|
2021-12-05 06:00:03 +02:00
|
|
|
from ArchieCore import Command, CommandsContext, CommandsContextDelegate
|
2021-08-22 17:24:30 +03:00
|
|
|
from General import Text2Speech
|
2021-10-24 22:26:09 +03:00
|
|
|
from ..Control import Control
|
2021-10-17 23:52:57 +03:00
|
|
|
from .MyTeleBot import MyTeleBot
|
2021-08-21 02:09:37 +03:00
|
|
|
|
2021-12-05 06:00:03 +02:00
|
|
|
class TelegramBot(Control, CommandsContextDelegate):
|
2021-08-21 02:09:37 +03:00
|
|
|
threads = []
|
|
|
|
online = True
|
|
|
|
voids = 0
|
|
|
|
memory = []
|
|
|
|
voice = Text2Speech.Engine()
|
2021-10-17 23:52:57 +03:00
|
|
|
bot = MyTeleBot(config.telebot)
|
2021-12-05 06:00:03 +02:00
|
|
|
commandsContext: CommandsContext
|
|
|
|
|
|
|
|
# Control
|
2021-08-21 02:09:37 +03:00
|
|
|
|
2021-11-28 20:01:53 +02:00
|
|
|
def __init__(self):
|
2021-12-05 06:00:03 +02:00
|
|
|
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-11-28 20:01:53 +02:00
|
|
|
|
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
|
|
|
|
2021-12-05 06:00:03 +02: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):
|
2021-12-05 06:00:03 +02:00
|
|
|
self.commandsContext.processString(msg.text.lower(), data = {'id': msg.chat.id})
|