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

74 lines
2.3 KiB
Python
Raw Normal View History

2021-08-21 02:09:37 +03:00
import time
import os
import config
from ArchieCore import Command, CommandsContextManager, CommandsContextManagerDelegate
from IO import Text2Speech
2022-01-07 21:05:42 +02:00
from Features.Media import YoutubePlayer, TorrentPlayer
from CallbackTeleBot import CallbackTeleBot
class TelegramBot(CommandsContextManagerDelegate):
2021-08-21 02:09:37 +03:00
online = True
voids = 0
voice = Text2Speech.Engine()
bot = CallbackTeleBot(config.telebot)
commandsContext: CommandsContextManager
# Control
2021-08-21 02:09:37 +03:00
def __init__(self):
self.commandsContext = CommandsContextManager(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
# CommandsContextManagerDelegate
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:
path = self.voice.generate(response.voice).path
voiceFile = open(path, 'rb')
try:
self.bot.send_voice(id, voiceFile)
finally:
voiceFile.close()
2021-08-21 02:09:37 +03:00
# Telebot
2021-08-21 02:09:37 +03:00
2022-01-07 21:05:42 +02:00
@bot.message_handler(commands = ['vlc', 'queue', 'cmd'])
2021-08-21 02:09:37 +03:00
def simple_commands(msg):
command = msg.text.replace('/cmd', '').replace('/vlc', 'vlc')
2022-01-07 21:05:42 +02:00
if '/queue' in msg.text: command = 'vlc ' + command.replace('/queue', '') + '--playlist-enqueue'
2021-08-21 02:09:37 +03:00
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):
2022-01-07 21:05:42 +02:00
if 'youtu' in msg.text:
YoutubePlayer(msg.text).play()
elif '.torrent' in msg.text:
TorrentPlayer.playUrl(msg.text)
else:
TelegramBot().commandsContext.processString(msg.text.lower(), data = {'id': msg.chat.id})