예제 #1
0
        def ev_tts(msg):
            text = msg.text or msg.caption
            text = re.sub(commands, '', text)

            if len(text):
                if translate_client and tts_client:
                    lang = google_detect_lang(text)
                    if lang and lang in google_tts_languages:
                        audio = tts(text, lang)
                        if audio:
                            bot.send_voice(
                                msg.chat.id,
                                voice=BytesIO(audio),
                                caption=text
                            )  #, reply_to_message_id=msg.message_id)
                        else:
                            bot.reply_to(
                                msg,
                                "Функционал недоступен или нет синтезатора для {lang}"
                                .format(lang=lang))
                    elif lang not in google_tts_languages:
                        bot.reply_to(
                            msg,
                            "Нет синтезатора для {lang}".format(lang=lang))
                    else:
                        bot.reply_to(msg, "Не удалось распознать язык.")
                else:
                    bot.reply_to(msg, "Функционал недоступен.")
예제 #2
0
        def callback_query(call):
            try:
                bot.answer_callback_query(call.id, "")
            except Exception as e:
                print(e)

            data = call.data
            print(data)
            hist_id = None
            what = None
            if data:
                try:
                    data = json.loads(data)
                    function = data['function']
                    if (function == 'tts'):
                        hist_id = data['id']
                        what = data['what']

                except Exception as e:
                    print("This is not tts:", call.data, e)
            if hist_id is not None and what:
                from plugins.google_translate import get_history_item
                translated = get_history_item(call.message.chat.id, hist_id)
                if not translated:
                    print("No such item in translation history:", hist_id)
                    return
                if what == "src":
                    lang = translated['src_lang']
                    text = translated['query']
                elif what == "target":
                    lang = translated['target_lang']
                    text = translated['result']
                else:
                    print("Cannot get `what` from callback. full callback:",
                          call.data)

                #print(call.message.chat.id)

                audio = tts(text, lang)
                if audio:
                    bot.send_voice(
                        call.message.chat.id,
                        voice=BytesIO(audio),
                        caption=text)  #, reply_to_message_id=msg.message_id)
                else:
                    bot.reply_to(
                        call.message,
                        "Функционал недоступен или нет синтезатора для {lang}".
                        format(lang=lang))
예제 #3
0
 def send_welcome(message):
     print(message.chat.type)
     if message.chat.type in ['group', 'channel', 'supergroup']:
         args = message.text.split(' ')
         args.pop(0)
         if len(args):
             if args[0] == 'alter':
                 args.pop(0)
                 alter_topic(message.chat.id, ' '.join(args))
             elif args[0] == 'rollback':
                 rollback_topic(message.chat.id)
             else:
                 set_topic(message.chat.id, ' '.join(args))
         else:
             bot.reply_to(message, "Topic is: {}".format(message.chat.title))
        def history(msg):
            text = msg.text or msg.caption
            text = re.sub(histregex, '', text)
            send = ''
            markup = False
            if len(text):
                item = get_history_item(msg.chat.id, text)
                if not item:
                    send = 'Cannot get item `{text}`. Type `!history` to get full history.'.format(
                        {'text': text})
                else:
                    send = '`{query}` {src_lang}<->{target_lang} `{result}`'.format(
                        **item)
                    btns = []
                    if item['src_lang'] in google_tts_languages:
                        btns.append(
                            types.InlineKeyboardButton(
                                'Say [' + item['src_lang'] + ']',
                                callback_data=json.dumps({
                                    'function': 'tts',
                                    'what': 'src',
                                    'id': item['id']
                                })))

                    if item['target_lang'] in google_tts_languages:
                        btns.append(
                            types.InlineKeyboardButton(
                                'Say [' + item['target_lang'] + ']',
                                callback_data=json.dumps({
                                    'function': 'tts',
                                    'what': 'target',
                                    'id': item['id']
                                })))

                    if len(btns):
                        markup = types.InlineKeyboardMarkup(row_width=2)
                        markup.add(*btns)
            else:
                full = get_history(msg.chat.id)
                send = ''
                if not len(full):
                    send = "History is empty."
                for item in full:
                    send += '\n{id} `{query}` {src_lang}<->{target_lang} `{result}`'.format(
                        **item)
            bot.reply_to(msg, send, reply_markup=markup, parse_mode="Markdown")
예제 #5
0
 def do_convert(msg):
     error = None
     args = None
     try:
         args = parse_args(msg.text)
     except Exception as e:
         error = e
     if args:
         try:
             args['result'] = str(
                 round(
                     currency_convert(args['amount'], args['from'],
                                      args['to']), 2))
         except Exception as e:
             error = e
     if not error:
         bot.reply_to(
             msg,
             "{amount} {from} = `{result}` {to}".format(**args),
             parse_mode="Markdown")
     else:
         bot.reply_to(msg, "{}".format(error), parse_mode="Markdown")
예제 #6
0
 def langs(msg):
     bot.reply_to(msg, ', '.join(google_tts_languages))
예제 #7
0
 def relayout(msg):
     if msg.reply_to_message:
         r = do_relayout(msg.reply_to_message.text)
         bot.reply_to(msg.reply_to_message, r)
예제 #8
0
 def send_welcome(message):
     if "text" in config:
         bot.reply_to(message, config["text"])
     else:
         bot.reply_to(message, "Hello, world!")