Example #1
0
def tl(message, text):
    if type(message) == int or type(message) == str and message[1:].isdigit():
        getlang = sql.get_lang(message)
        if getlang == 'None' or not getlang:
            getlang = 'en'
    else:
        getlang = sql.get_lang(message.chat.id)
        if getlang == 'None' or not getlang:
            if message.from_user.language_code:
                if message.from_user.language_code in LOADED_LANGS_ID:
                    sql.set_lang(message.chat.id,
                                 message.from_user.language_code)
                    getlang = message.from_user.language_code
                else:
                    sql.set_lang(message.chat.id, 'en')
                    getlang = 'en'
            else:
                sql.set_lang(message.chat.id, 'en')
                getlang = 'en'

    getlangid = {}
    for x in LOADED_LANGS_ID:
        getlangid[x] = x

    if str(getlang) == 'pt':
        get = getattr(FUNC_LANG['pt'], 'pt')
        if text in tuple(get):
            return get.get(text)
        if text in ("RUN_STRINGS", "SLAP_TEMPLATES", "ITEMS", "THROW", "HIT",
                    "RAMALAN_STRINGS", "RAMALAN_FIRST"):
            runstr = getattr(FUNC_LANG['pt'], text)
            return runstr
        return text
    elif str(getlang) in LOADED_LANGS_ID:
        func = getattr(FUNC_LANG[getlang], getlang)
        if text in ("RUN_STRINGS", "SLAP_TEMPLATES", "ITEMS", "THROW", "HIT",
                    "RAMALAN_STRINGS", "RAMALAN_FIRST"):
            runstr = getattr(FUNC_LANG[getlang], text)
            return runstr
        langtxt = func.get(text)
        if not langtxt:
            LOGGER.warning(
                "Can't get translated string for lang '{}' ('{}')".format(
                    str(getlang), text))
            langtxt = text
        return langtxt
    else:
        sql.set_lang(message.chat.id, 'en')
        get = getattr(FUNC_LANG['en'], 'en')
        if text in tuple(get):
            return get.get(text)
        if text in ("RUN_STRINGS", "SLAP_TEMPLATES", "ITEMS", "THROW", "HIT",
                    "RAMALAN_STRINGS", "RAMALAN_FIRST"):
            runstr = getattr(FUNC_LANG['en'], text)
            return runstr
        return text
def wiki(update, context):
    chat_id = update.effective_chat.id
    args = update.effective_message.text.split(None, 1)
    teks = args[1]
    getlang = langsql.get_lang(chat_id)
    if str(getlang) == "pt":
        wikipedia.set_lang("pt")
    else:
        wikipedia.set_lang("en")
    try:
        pagewiki = wikipedia.page(teks)
    except wikipedia.exceptions.PageError:
        send_message(update.effective_message, tl(update.effective_message, "Results not found"))
        return
    except wikipedia.exceptions.DisambiguationError as refer:
        rujuk = str(refer).split("\n")
        if len(rujuk) >= 6:
            batas = 6
        else:
            batas = len(rujuk)
        teks = ""
        for x in range(batas):
            if x == 0:
                if getlang == "pt":
                    teks += rujuk[x].replace('may refer to', 'pode se referir a') + "\n"
                else:
                    teks += rujuk[x] + "\n"
            else:
                teks += "- `" + rujuk[x] + "`\n"
        send_message(update.effective_message, teks, parse_mode="markdown")
        return
    except IndexError:
        send_message(update.effective_message,
                     tl(update.effective_message, "Write a message to search from the wikipedia source"))
        return
    judul = pagewiki.title
    summary = pagewiki.summary
    if update.effective_message.chat.type == "private":
        send_message(update.effective_message,
                     tl(update.effective_message, "Results of {} is:\n\n<b>{}</b>\n{}").format(teks, judul, summary),
                     parse_mode=ParseMode.HTML)
    else:
        if len(summary) >= 200:
            judul = pagewiki.title
            summary = summary[:200] + "..."
            button = InlineKeyboardMarkup([[InlineKeyboardButton(text=tl(update.effective_message, "Read More..."),
                                                                 url="t.me/{}?start=wiki-{}".format(
                                                                     context.bot.username, teks.replace(' ', '_')))]])
        else:
            button = None
        send_message(update.effective_message,
                     tl(update.effective_message, "Results of {} is:\n\n<b>{}</b>\n{}").format(teks, judul, summary),
                     parse_mode=ParseMode.HTML, reply_markup=button)
Example #3
0
def set_language(update, context):
    msg = update.effective_message  # type: Optional[Message]
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]

    getlang = sql.get_lang(chat.id)
    if getlang == 'None' or not getlang:
        if msg.from_user.language_code:
            sql.set_lang(msg.chat.id, msg.from_user.language_code)
            getlang = msg.from_user.language_code
        else:
            sql.set_lang(msg.chat.id, 'en')
            getlang = 'en'
    loaded_langs = []
    tmp_list = []
    counter = 0

    for x in LOADED_LANGS_ID:
        counter += 1
        tmp_list.append(
            InlineKeyboardButton(LANGS_TEXT[x],
                                 callback_data="set_lang({})".format(x)))
        if counter % 2 == 0:
            loaded_langs.append(tmp_list)
            tmp_list = []
        if counter == len(LOADED_LANGS_ID):
            loaded_langs.append(tmp_list)

    keyboard = InlineKeyboardMarkup(loaded_langs)

    if chat.title:
        chatname = chat.title
    else:
        if chat.type == "private":
            chatname = user.first_name
        else:
            chatname = tl(update.effective_message, "current in this chat")

    currlang = LANGS_TEXT[getlang] if LANGS_TEXT.get(
        getlang) else "(Deleted langs)"
    send_message(
        update.effective_message,
        tl(msg,
           "Current language in *{}* is:\n{}.\n\nSelect language:").format(
               chatname, currlang),
        parse_mode="markdown",
        reply_markup=keyboard)
Example #4
0
def translate(update, context):
    msg = update.effective_message
    getlang = langsql.get_lang(update.effective_message.from_user.id)
    try:
        if msg.reply_to_message and msg.reply_to_message.text:
            args = update.effective_message.text.split()
            if len(args) >= 2:
                target = args[1]
                if "-" in target:
                    target2 = target.split("-")[1]
                    target = target.split("-")[0]
                else:
                    target2 = None
            else:
                if getlang:
                    target = getlang
                    target2 = None
                else:
                    raise IndexError
            teks = msg.reply_to_message.text
            exclude_list = UNICODE_EMOJI.keys()
            for emoji in exclude_list:
                if emoji in teks:
                    teks = teks.replace(emoji, '')
            trl = Translator()
            if target2 is None:
                deteksibahasa = trl.detect(teks)
                tekstr = trl.translate(teks, dest=target)
                send_message(
                    update.effective_message,
                    tl(update.effective_message,
                       "Diterjemahkan dari `{}` ke `{}`:\n`{}`").format(
                           deteksibahasa.lang, target, tekstr.text),
                    parse_mode=ParseMode.MARKDOWN)
            else:
                tekstr = trl.translate(teks, dest=target2, src=target)
                send_message(
                    update.effective_message,
                    tl(update.effective_message,
                       "Diterjemahkan dari `{}` ke `{}`:\n`{}`").format(
                           target, target2, tekstr.text),
                    parse_mode=ParseMode.MARKDOWN)

        else:
            args = update.effective_message.text.split(None, 2)
            if len(args) != 1:
                target = args[1]
                teks = args[2]
                target2 = None
                if "-" in target:
                    target2 = target.split("-")[1]
                    target = target.split("-")[0]
            else:
                target = getlang
                teks = args[1]
            exclude_list = UNICODE_EMOJI.keys()
            for emoji in exclude_list:
                if emoji in teks:
                    teks = teks.replace(emoji, '')
            trl = Translator()
            if target2 is None:
                deteksibahasa = trl.detect(teks)
                tekstr = trl.translate(teks, dest=target)
                return send_message(
                    update.effective_message,
                    tl(update.effective_message,
                       "Diterjemahkan dari `{}` ke `{}`:\n`{}`").format(
                           deteksibahasa.lang, target, tekstr.text),
                    parse_mode=ParseMode.MARKDOWN)
            else:
                tekstr = trl.translate(teks, dest=target2, src=target)
                send_message(
                    update.effective_message,
                    tl(update.effective_message,
                       "Diterjemahkan dari `{}` ke `{}`:\n`{}`").format(
                           target, target2, tekstr.text),
                    parse_mode=ParseMode.MARKDOWN)
    except IndexError:
        send_message(
            update.effective_message,
            tl(
                update.effective_message,
                "Balas pesan atau tulis pesan dari bahasa lain untuk "
                "diterjemahkan kedalam bahasa yang di dituju\n\n"
                "Contoh: `/tr en-id` untuk menerjemahkan dari Bahasa inggris ke Bahasa Indonesia\n"
                "Atau gunakan: `/tr id` untuk deteksi otomatis dan menerjemahkannya kedalam bahasa indonesia"
            ),
            parse_mode="markdown")
    except ValueError:
        send_message(
            update.effective_message,
            tl(update.effective_message,
               "Bahasa yang di tuju tidak ditemukan!"))
    else:
        return
Example #5
0
def start(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    # query = update.callback_query #Unused variable
    if update.effective_chat.type == "private":
        if len(args) >= 1:
            if args[0].lower() == "help":
                send_help(
                    update.effective_chat.id,
                    tld(chat.id, "send-help").format(
                        dispatcher.bot.first_name,
                        "" if not ALLOW_EXCL else tld(
                            chat.id,
                            "\nAll commands can either be used with `/` or `!`.\n"
                        )))

            elif args[0].lower().startswith("stngs_"):
                match = re.match("stngs_(.*)", args[0].lower())
                chat = dispatcher.bot.getChat(match.group(1))

                if is_user_admin(chat, update.effective_user.id):
                    send_settings(match.group(1),
                                  update.effective_user.id,
                                  update,
                                  user=False)
                else:
                    send_settings(match.group(1),
                                  update.effective_user.id,
                                  update,
                                  user=True)

            elif args[0][1:].isdigit() and "rules" in IMPORTED:
                IMPORTED["rules"].send_rules(update, args[0], from_pm=True)

            elif args[0].lower() == "controlpanel":
                control_panel(bot, update)

            elif args[0][:4] == "wiki":
                wiki = args[0].split("-")[1].replace('_', ' ')
                message = update.effective_message
                getlang = langsql.get_lang(message)
                if getlang == "en":
                    wikipedia.set_lang("en")
                pagewiki = wikipedia.page(wiki)
                judul = pagewiki.title
                summary = pagewiki.summary
                if len(summary) >= 4096:
                    summary = summary[:4000] + "..."
                message.reply_text("<b>{}</b>\n{}".format(judul, summary),
                                   parse_mode=ParseMode.HTML,
                                   reply_markup=InlineKeyboardMarkup([[
                                       InlineKeyboardButton(text=(tld(
                                           chat.id, "Read it on Wikipedia")),
                                                            url=pagewiki.url)
                                   ]]))

        else:
            send_start(bot, update)
    else:
        try:
            update.effective_message.reply_text((tld(
                chat.id,
                "Halo guys! Saya masih aktif :3 PM saya jika kamu punya pertanyaan tentang saya!"
            )))
        except:
            print("Nut")
def start(update, context):
    if update.effective_chat.type == "private":
        args = context.args
        if len(args) >= 1:
            if args[0].lower() == "help":
                send_help(update.effective_chat.id, tl(update.effective_message, HELP_STRINGS))

            elif args[0].lower() == "get_notes":
                update.effective_message.reply_text(tl(update.effective_message, "Now you can get notes in group."))

            elif args[0].lower().startswith("stngs_"):
                match = re.match("stngs_(.*)", args[0].lower())
                chat = dispatcher.bot.getChat(match.group(1))

                if is_user_admin(chat, update.effective_user.id):
                    send_settings(match.group(1), update.effective_user.id, False)
                else:
                    send_settings(match.group(1), update.effective_user.id, True)

            elif args[0][1:].isdigit() and "rules" in IMPORTED:
                IMPORTED["rules"].send_rules(update, args[0], from_pm=True)

            elif args[0][:4] == "wiki":
                wiki = args[0].split("-")[1].replace('_', ' ')
                message = update.effective_message
                getlang = langsql.get_lang(message)
                if getlang == "pt":
                    wikipedia.set_lang("pt")
                pagewiki = wikipedia.page(wiki)
                judul = pagewiki.title
                summary = pagewiki.summary
                if len(summary) >= 4096:
                    summary = summary[:4000] + "..."
                message.reply_text("<b>{}</b>\n{}".format(judul, summary), parse_mode=ParseMode.HTML,
                                   reply_markup=InlineKeyboardMarkup(
                                       [[InlineKeyboardButton(text=tl(update.effective_message, "Read on Wikipedia"),
                                                              url=pagewiki.url)]]))

            elif args[0][:6].lower() == "verify":
                chat_id = args[0].split("_")[1]
                verify_welcome(update, context, chat_id)

            elif args[0][:6].lower() == "verify":
                chat_id = args[0].split("_")[1]
                verify_welcome(update, context, chat_id)

        else:
            first_name = update.effective_user.first_name
            buttons = InlineKeyboardMarkup(
                [[InlineKeyboardButton(text="πŸ‡ΊπŸ‡Έ Language", callback_data="main_setlang"),
                  InlineKeyboardButton(text="βš™οΈ Connections", callback_data="main_connect")],
                 [InlineKeyboardButton(text="πŸ“‘ Shinchan News", url="https://telegram.me/ShinchanNews"),
                  InlineKeyboardButton(text="❓ Help", url="https://t.me/Shinchan_RoBot?start=help")]])
            update.effective_message.reply_text(
                tl(update.effective_message, PM_START_TEXT).format(escape_markdown(first_name),
                                                                   escape_markdown(context.bot.first_name), OWNER_ID),
                disable_web_page_preview=True,
                parse_mode=ParseMode.MARKDOWN,
                reply_markup=buttons)
    else:
        update.effective_message.reply_text(tl(update.effective_message, "Is there anything I can help? 😊"))