コード例 #1
0
ファイル: song.py プロジェクト: sushantgirdhar/SongBot
async def s(client, message):
    chat_id = message.chat.id
    user_id = message.from_user["id"]
    args = get_arg(message) + " " + "song"
    if args.startswith(" "):
        await message.reply("Enter a song name. Check /help")
        return ""
    status = await message.reply("Rukja Bsdk....")
    video_link = yt_search(args)
    if not video_link:
        await status.edit("Song not found.")
        return ""
    yt = YouTube(video_link)
    audio = yt.streams.filter(only_audio=True).first()
    try:
        download = audio.download(filename=f"{str(user_id)}")
    except Exception as ex:
        await status.edit("Failed to download song")
        LOGGER.error(ex)
        return ""
    rename = os.rename(download, f"{str(user_id)}.mp3")
    await app.send_chat_action(message.chat.id, "upload_audio")
    await app.send_audio(
        chat_id=message.chat.id,
        audio=f"{str(user_id)}.mp3",
        duration=int(yt.length),
        thumb=f"https://telegra.ph/file/96c37616e91e501292040.jpg",
        caption="**Uploaded By @SushantGirdhar**",
        title=str(yt.title),
        performer=str(yt.author),
        reply_to_message_id=message.message_id,
    )
    await status.delete()
    os.remove(f"{str(user_id)}.mp3")
コード例 #2
0
async def song(client, message):
    chat_id = message.chat.id
    user_id = message.from_user["id"]
    add_chat_to_db(str(chat_id))
    args = get_arg(message) + " " + "song"
    if args.startswith(" "):
        await message.reply("Enter a song name. Check /help")
        return ""
    status = await message.reply("Processing...")
    video_link = yt_search(args)
    if not video_link:
        await status.edit("Song not found.")
        return ""
    yt = YouTube(video_link)
    audio = yt.streams.filter(only_audio=True).first()
    try:
        download = audio.download(filename=f"{str(user_id)}")
    except Exception as ex:
        await status.edit("Failed to download song")
        LOGGER.error(ex)
        return ""
    rename = os.rename(download, f"{str(user_id)}.mp3")
    await app.send_chat_action(message.chat.id, "upload_audio")
    await app.send_audio(
        chat_id=message.chat.id,
        audio=f"{str(user_id)}.mp3",
        duration=int(yt.length),
        title=str(yt.title),
        performer=str(yt.author),
        reply_to_message_id=message.message_id,
    )
    await status.delete()
    os.remove(f"{str(user_id)}.mp3")
コード例 #3
0
def error_handler(update, context):
    """Log the error and send a telegram message to notify the developer."""
    cmd, args = update.effective_message.text.split(None, 1)
    LOGGER.error(msg="Error found, check dump below:", exc_info=context.error)

    # traceback.format_exception returns the usual python message about an exception, but as a
    # list of strings rather than a single string, so we have to join them together.
    tb_list = traceback.format_exception(None, context.error,
                                         context.error.__traceback__)
    trace = "".join(tb_list)

    # lets try to get as much information from the telegram update as possible
    payload = f"\n<b>- Command</b>: <code>{cmd}</code>"
    payload += f"\n<b>- Arguments</b>: <code>{args}</code>"
    payload += f"\n<b>- Error message</b>:\n<code>{context.error}</code>"
    # normally, we always have a user. If not, its either a channel or a poll update.
    if update.effective_user:
        payload += f" \n<b>- User</b>: {mention_html(update.effective_user.id, update.effective_user.first_name)}"
    # there are more situations when you don't get a chat
    if update.effective_chat:
        if update.effective_chat.title == None:
            payload += f" \n<b>- Chat</b>: <i>Bot PM</i>"
        else:
            payload += (
                f" \n<b>- Chat</b>: <i>{update.effective_chat.title}</i>")
    # but only one where you have an empty payload by now: A poll (buuuh)
    if update.poll:
        payload += f" \n<b>- Poll id</b>: {update.poll.id}."
    # lets put this in a "well" formatted text
    text = f"<b>Error found while handling an update!</b> {payload}"

    # now paste the error (trace) in nekobin and make buttons
    # with url of log, as log in telegram message is hardly readable..
    key = (requests.post(
        "https://nekobin.com/api/documents",
        json={
            "content":
            f"{trace}\n\n{json.dumps(update.to_dict(), indent=2, ensure_ascii=False)}"
        },
    ).json().get("result").get("key"))

    markup = InlineKeyboardMarkup([[
        InlineKeyboardButton(
            text="Full traceback on nekobin",
            url=f"https://nekobin.com/{key}.py",
        ),
        # InlineKeyboardButton(
        #     text="Send traceback as message",
        #     ,
        # ),
    ]])
    context.bot.send_message(OWNER_ID,
                             text,
                             reply_markup=markup,
                             parse_mode="html")