├── LICENSE ├── README.md └── speechbot.py /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Martijn Rondeel 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | SpeechBot 2 | = 3 | 4 | SpeechBot is a Telegram bot written in Python that converts anything you say into a voice message using Google's Text-to-Speech API. 5 | 6 | Installation 7 | = 8 | 9 | SpeechBot requires: 10 | - Python 2.7 11 | - [Flask](http://flask.pocoo.org/docs/0.10/) 12 | - [redis-py](https://github.com/andymccurdy/redis-py) 13 | - Linux 14 | 15 | Download the bot: 16 | ``` 17 | git clone https://github.com/martijnrondeel/SpeechBot 18 | ``` 19 | (or download the [.zip](https://github.com/martijnrondeel/SpeechBot/archive/master.zip)) 20 | 21 | Run the bot (don't forget to edit the code first to add your token, SSL certs, etc.): 22 | ``` 23 | cd SpeechBot 24 | python speechbot.py 25 | ``` 26 | 27 | Contributing 28 | = 29 | 30 | Please feel free to contribute! This is my first open-source Python project, I have probably made silly mistakes somewhere ;) 31 | -------------------------------------------------------------------------------- /speechbot.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request 2 | import json 3 | import requests 4 | import redis 5 | 6 | app = Flask(__name__) 7 | 8 | file_name = "speech.ogg" 9 | languages = ["nl", "en-GB", "en-US", "fr", "de", "it", "ru", "cs", "pl", "es", "tr", "pt", "zh", "ar", "sv", "ja"] 10 | 11 | token = "token here" #Bot token (Example: 93181085:AAELcePZ1qabYrDiu0t1PVPuw1HI0zXzXmq) 12 | cert = "example.crt" #SSL certificate (Can not be self-signed) 13 | key = "example.key" #SSL key (Can not be self-signed) 14 | 15 | api = "https://api.telegram.org/bot" + token 16 | 17 | #Pretend we are human 18 | headers = {"Host": "translate.google.com", 19 | "Referer": "http://www.gstatic.com/translate/sound_player2.swf", 20 | "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) " 21 | "AppleWebKit/535.19 (KHTML, like Gecko) " 22 | "Chrome/18.0.1025.163 Safari/535.19" 23 | } 24 | 25 | #Redis database to store language choice 26 | db = redis.StrictRedis(host='localhost', port=6379, db=0) 27 | 28 | #How long we should remember language choice (in days) 29 | remember = 5 30 | 31 | #Send text to chat 32 | def sendMessage(chat_id, text): 33 | payload = {'chat_id': chat_id, 'text': text} 34 | r = requests.post(api + "/sendMessage", params=payload) 35 | return 36 | 37 | #Send voice message to chat 38 | def sendVoice(chat_id, language, text): 39 | payload = {'tl': language, 'q': text} 40 | r = requests.get("http://translate.google.com/translate_tts", params=payload, headers=headers) 41 | 42 | with open(file_name, 'wb') as file: 43 | file.write(r.content) 44 | 45 | payload = {'chat_id': chat_id} 46 | file = {'audio': open('speech.ogg', 'rb')} 47 | 48 | r = requests.post(api + "/sendAudio", params=payload, files=file) 49 | return 50 | 51 | #Where the magic happens 52 | @app.route('/telegram',methods=['POST']) 53 | def main(): 54 | data = json.loads(request.data) 55 | 56 | # We are only interested in text messages 57 | if 'text' in data['message']: 58 | message = data['message']['text'].encode('utf-8').strip() 59 | chat_id = data['message']['chat']['id'] 60 | 61 | # /start 62 | if message == "/start": 63 | sendMessage(chat_id, "Use the speech command to let me do something! (Example: /speech I like apples)") 64 | return "OK" 65 | 66 | # /speech 67 | if message.startswith("/speech "): 68 | 69 | submittedText = message.replace("/speech ","",1) 70 | 71 | if db.get(chat_id) is not None: 72 | selectedLanguage = db.get(data['message']['chat']['id']) 73 | db.expire(data['message']['chat']['id'], remember * 3600) 74 | else: 75 | selectedLanguage = 'en-GB' #Default to English if no language has been set 76 | 77 | if len(submittedText) <= 99: 78 | sendVoice(chat_id, selectedLanguage, submittedText) 79 | else: 80 | sendMessage(chat_id, "Please use less than 100 characters") 81 | return "OK" 82 | elif message == "/speech": 83 | sendMessage(chat_id, "Use the command like this: /speech Hello, I like apples") 84 | return "OK" 85 | 86 | # /help 87 | if message == "/help": 88 | sendMessage(chat_id, """I support the following commands: \n 89 | /speech - Translate your text into a voice message 90 | /language - Change the language of the bot 91 | /help - Show information about the commands 92 | /about - Show information about the bot""") 93 | return "OK" 94 | 95 | # /about 96 | if message == "/about": 97 | sendMessage(chat_id, "Source code: https://github.com/lasermarty/SpeechBot") 98 | return "OK" 99 | 100 | # /language 101 | if message == "/language": 102 | sendMessage(chat_id, """Available language codes are: \n 103 | nl (Dutch) 104 | en-GB (English UK) 105 | en-US (English US) 106 | fr (French) 107 | de (German) 108 | it (Italian) 109 | ru (Russian) 110 | cs (Czech) 111 | pl (Polish) 112 | es (Spanish) 113 | tr (Turkish) 114 | pt (Portuguese) 115 | zh (Chinese) 116 | ar (Arabic) 117 | sv (Swedish) 118 | ja (Japanese)""") 119 | return "OK" 120 | elif message.startswith("/language "): 121 | 122 | language = message.replace("/language ","",1) 123 | 124 | if language in languages: 125 | db.set(data['message']['chat']['id'], language) 126 | db.expire(data['message']['chat']['id'], remember * 3600) 127 | sendMessage(chat_id, "Successfully set language to " + language) 128 | return "OK" 129 | else: 130 | sendMessage(chat_id, "Please use a correct language code (nl, en-GB, en-US, fr, de, it, ru, cs, pl, es, tr, pt, zh, ar, sv, ja)") 131 | return "OK" 132 | return "OK" 133 | 134 | if __name__ == '__main__': 135 | app.run('0.0.0.0', debug=False, port=443, ssl_context=(cert, key), threaded=True) #Port can be 443, 80, 88, 8443 136 | --------------------------------------------------------------------------------