|
1
|
|
|
#!/usr/bin/python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
from telegram.ext import CommandHandler |
|
4
|
|
|
from modules.logging import log_command |
|
5
|
|
|
from telegram import ChatAction |
|
6
|
|
|
from datetime import datetime |
|
7
|
|
|
from gtts import gTTS |
|
8
|
|
|
import os |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def module_init(gd): |
|
12
|
|
|
global path |
|
13
|
|
|
path = gd.config["path"] |
|
14
|
|
|
commands = gd.config["commands"] |
|
15
|
|
|
for command in commands: |
|
16
|
|
|
gd.dp.add_handler(CommandHandler(command, tts, pass_args=True)) |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def tts(bot, update, args): |
|
20
|
|
|
current_time = datetime.strftime(datetime.now(), "%d.%m.%Y %H:%M:%S") |
|
21
|
|
|
filename = datetime.now().strftime("%d%m%y-%H%M%S%f") |
|
22
|
|
|
reply = update.message.reply_to_message |
|
23
|
|
|
if reply is None: |
|
24
|
|
|
text = "".join(args) |
|
25
|
|
|
elif reply.text is not None: |
|
26
|
|
|
text = reply.text |
|
27
|
|
|
if len(text) == 0: |
|
|
|
|
|
|
28
|
|
|
update.message.reply_text("Type in some text ^^") |
|
29
|
|
|
return |
|
30
|
|
|
update.message.chat.send_action(ChatAction.RECORD_AUDIO) |
|
31
|
|
|
lang="en" |
|
32
|
|
|
tts = gTTS(text, lang) |
|
33
|
|
|
tts.save(path + filename + ".mp3") |
|
|
|
|
|
|
34
|
|
|
with open(path + filename + ".mp3", "rb") as f: |
|
35
|
|
|
linelist = list(f) |
|
36
|
|
|
linecount = len(linelist) |
|
37
|
|
|
if linecount == 1: |
|
38
|
|
|
update.message.chat.send_action(ChatAction.RECORD_AUDIO) |
|
39
|
|
|
lang = "ru" |
|
40
|
|
|
tts = gTTS(text, lang) |
|
41
|
|
|
tts.save(path + filename + ".mp3") |
|
42
|
|
|
with open(path + filename + ".mp3", "rb") as speech: |
|
43
|
|
|
update.message.reply_voice(speech, quote=False) |
|
44
|
|
|
print(current_time, ">", "/say", ">", update.message.from_user.username) |
|
45
|
|
|
os.remove(path+filename+".mp3") |
|
46
|
|
|
log_command(bot, update, current_time, "say") |
|
47
|
|
|
|