From 622f003ec0c1453dfdd95ac6015d12982eba4064 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Wed, 2 Mar 2022 22:42:28 -0500 Subject: [PATCH] [+] Sound bot --- requirements.txt | 4 ++++ src/SoundBot.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/SoundBot.py diff --git a/requirements.txt b/requirements.txt index b642eb9..39c7331 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,6 @@ python-telegram-bot==13.11 selenium==4.1.2 + +pygame==2.1.2 +fastapi +uvicorn[standard] diff --git a/src/SoundBot.py b/src/SoundBot.py new file mode 100644 index 0000000..ce71c30 --- /dev/null +++ b/src/SoundBot.py @@ -0,0 +1,39 @@ +import os + +from fastapi import FastAPI +from pygame.mixer import Sound +from telegram.ext import Updater, Dispatcher, CommandHandler + +# Config +AUDIO = 'alarm.mp3' +TG_TOKEN = os.environ['TG_TOKEN'] + +# Internal variables +app = FastAPI() +sounds: list[Sound] = [] + + +@app.get('/alarm') +async def alarm(): + s = Sound(AUDIO) + s.play() + return {'message': 'sound played'} + + +@app.get('/stop') +async def stop(): + for s in sounds: + s.stop() + sounds.clear() + + +if __name__ == '__main__': + updater = Updater(token=TG_TOKEN) + dispatcher: Dispatcher = updater.dispatcher + bot = updater.bot + + dispatcher.add_handler(CommandHandler('stop', stop)) + dispatcher.add_handler(CommandHandler('alarm', alarm)) + + print('Starting...') + updater.start_polling()