A simple telegram bot written in python to reverse voice messages it recieves.
You can use this link to test the bot.
Before anything you can clone the repository using:
git clone https://github.com/pharzan/barax_bot.git
- Using the bot father create a Telegram bot to obtain a Token explained here. The bot father will chat with you through the process of obtaining a token. Then replace the obtained token in the source code. The token should look something like this:
123456789:ABCdefGhIJKlmNoPQRsTUVwxyZ
- Install Telepot for Python to use the Telegram API in python.
pip install telepot
pip install telepot --upgrade # UPGRADE
Or using Easy install:
easy_install telepot
easy_install --upgrade telepot # UPGRADE
- Use python to run the script.
python barax_bot.py
The bot uses subprocesses in python to execute a ffmpeg, handy Linux utility for audio and video.
The bot listens to any messages that are recieved.
def on_chat_message(self, msg):
content_type, chat_type, chat_id, msg_date, msg_id = telepot.glance(msg,flavor='chat',long=True)
If the recieved message is a voice message and the duration is less than 30 seconds it downloads the file.
file_name = msg['voice']['file_id']
bot.download_file(msg['voice']['file_id'],"{0}.ogg".format(file_name))
Finally it executes ffmpeg in the shell to simply reverses the ogg file and creates an MP3 to send to the user.
shell_cmd = "yes y | ffmpeg -i {0} -vf reverse -af areverse -f mp3 {1}.mp3".format(my_file,file_name)
p = subprocess.Popen(shell_cmd, shell=True,stdout=subprocess.PIPE)
....
bot.sendVoice(chat_id,open(file_name+'.mp3', 'rb'))
Notice: The chat ID is a unique ID created by Telegram which is an identifier between the bot and user chat.