-
Notifications
You must be signed in to change notification settings - Fork 1
/
google_asst.py
125 lines (118 loc) · 4.13 KB
/
google_asst.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import asyncio
import os
import subprocess
import datetime
import emoji
from googletrans import Translator
from gtts import gTTS
from . import *
@bot.on(d3vil_cmd(pattern="trt ?(.*)"))
@bot.on(sudo_cmd(pattern="trt ?(.*)", allow_sudo=True))
async def _(event):
if event.fwd_from:
return
if "trim" in event.raw_text:
return
input_str = event.pattern_match.group(1)
if event.reply_to_msg_id:
previous_message = await event.get_reply_message()
text = previous_message.message
lan = input_str or "en"
elif "-" in input_str:
lan, text = input_str.split("-")
else:
await eod(
event,
f"`{hl}trt LanguageCode - message` or `{hl}trt LanguageCode as reply to a message.`\n\nTry `{hl}trc` to get all language codes",
7,
)
return
text = emoji.demojize(text.strip())
lan = lan.strip()
translator = Translator()
try:
translated = translator.translate(text, dest=lan)
after_tr_text = translated.text
output_str = """**Translated**\nFrom {} to {}
{}""".format(
translated.src, lan, after_tr_text
)
await edit_or_reply(event, output_str)
except Exception as exc:
await edit_or_reply(event, str(exc))
@bot.on(d3vil_cmd(pattern=r"trc", outgoing=True))
@bot.on(sudo_cmd(pattern=r"trc", allow_sudo=True))
async def _(d3vil):
if d3vil.fwd_from:
return
await edit_or_reply(d3vil, "**All The Language Codes Can Be Found** \n ⚡ [Here](https://telegra.ph/SfMæisér--𐌷𐌴ࠋࠋ𐌱𐍈𐌸-𐌾𐌰𐍀𐌾-06-04) ⚡", link_preview=False)
@bot.on(d3vil_cmd(pattern="voice (.*)"))
@bot.on(sudo_cmd(pattern="voice (.*)", allow_sudo=True))
async def _(event):
if event.fwd_from:
return
input_str = event.pattern_match.group(1)
start = datetime.datetime.now()
if event.reply_to_msg_id:
previous_message = await event.get_reply_message()
text = previous_message.message
lan = input_str
elif "-" in input_str:
lan, text = input_str.split("-")
else:
await eod(event, "Invalid Syntax. Module stopping.")
return
text = text.strip()
lan = lan.strip()
if not os.path.isdir(Config.TMP_DOWNLOAD_DIRECTORY):
os.makedirs(Config.TMP_DOWNLOAD_DIRECTORY)
required_file_name = Config.TMP_DOWNLOAD_DIRECTORY + "voice.ogg"
try:
tts = gTTS(text, lang=lan)
tts.save(required_file_name)
command_to_execute = [
"ffmpeg",
"-i",
required_file_name,
"-map",
"0:a",
"-codec:a",
"libopus",
"-b:a",
"100k",
"-vbr",
"on",
required_file_name + ".opus",
]
try:
t_response = subprocess.check_output(
command_to_execute, stderr=subprocess.STDOUT
)
except (subprocess.CalledProcessError, NameError, FileNotFoundError) as exc:
await event.edit(str(exc))
# continue sending required_file_name
else:
os.remove(required_file_name)
required_file_name = required_file_name + ".opus"
end = datetime.datetime.now()
ms = (end - start).seconds
await borg.send_file(
event.chat_id,
required_file_name,
reply_to=event.message.reply_to_msg_id,
allow_cache=False,
voice_note=True,
)
os.remove(required_file_name)
await eor(event, "Processed {} ({}) in {} seconds!".format(text[0:97], lan, ms))
await asyncio.sleep(5)
await event.delete()
except Exception as e:
await eod(event, str(e), 10)
CmdHelp("google_asst").add_command(
"voice", "<reply to a msg> <lang code>", "Sends the replied msg content in audio format."
).add_command(
"trt", "<lang code> <reply to msg>", "Translates the replied message to desired language code. Type '.trc' to get all the language codes", f"trt en - d3vilo | {hl}trt en <reply to msg>"
).add_command(
"trc", None, "Gets all the possible language codes for google translate module"
).add()