Completed
Push — master ( 0848f6...9a8d9e )
by Anas
55s
created

tts()   B

Complexity

Conditions 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 0
loc 20
rs 8.5454
1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from telegram.ext import CommandHandler
4
from telegram import ChatAction
5
from gtts import gTTS
6
import datetime
7
import yaml
8
9
10
def handler(dp):
11
    dp.add_handler(CommandHandler("say", tts, pass_args=True))
12
13
with open("config.yml", "r") as f:
14
    path = yaml.load(f)["path"]["tts"]
15
16
17
def tts(bot, update, args):
0 ignored issues
show
Unused Code introduced by
The argument bot seems to be unused.
Loading history...
18
    text = "".join(args)
19
    if len(text) == 0:
20
        update.message.reply_text("Type in some text ^^")
21
        return
22
    update.message.chat.send_action(ChatAction.RECORD_AUDIO)
23
    lang="en"
24
    tts = gTTS(text, lang)
25
    tts.save(path + "voice.mp3")
26
    with open(path + "voice.mp3", "rb") as f:
27
        linelist = list(f)
28
        linecount = len(linelist)
29
    if linecount == 1:
30
        update.message.chat.send_action(ChatAction.RECORD_AUDIO)
31
        lang = "ru"
32
        tts = gTTS(text, lang)
33
        tts.save(path + "voice.mp3")
34
    with open(path + "voice.mp3", "rb") as speech:
35
        update.message.reply_voice(speech, quote=False)
36
    print(datetime.datetime.now(), ">>>", "Done tts", ">>>", update.message.from_user.username)