【问题标题】:How to send a gif with an inline keyboard when start is pressed using python-telegram-bot?使用python-telegram-bot按下开始时如何使用内联键盘发送gif?
【发布时间】:2019-08-14 17:35:04
【问题描述】:

我正在尝试制作一个菜单机器人。一切正常,但我不能在键盘和消息之前放一个 GIF 来装饰。

我使用了.inputmedia.document 的一些变体 来自here

我什么都不知道,python 知识为零。我只能通过在互联网上阅读来理解。我真的不明白如何表达它。

from telegram.ext import Updater
from telegram.ext import CommandHandler, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
import emoji
def start(bot, update):
  update.message.reply_text(main_menu_message(),
                            reply_markup=main_menu_keyboard())

def main_menu(bot, update):
  query = update.callback_query
  bot.edit_message_text(chat_id=query.message.chat_id,
                        message_id=query.message.message_id,
                        text=main_menu_message(),
                        reply_markup=main_menu_keyboard())

def first_menu(bot, update):
  query = update.callback_query
  bot.edit_message_text(chat_id=query.message.chat_id,
                        message_id=query.message.message_id,
                        text=first_menu_message(),
                        reply_markup=first_menu_keyboard())

我几乎只需要一个示例,如何用一些文本和标记键盘来表达inputgif 命令。谢谢!

【问题讨论】:

  • 您好,我的回答解决了您的问题吗?如果是这样,请随时接受。

标签: python telegram telegram-bot python-telegram-bot


【解决方案1】:

你的意思是这样的?

如果,请继续阅读!如果没有,请在 cmets 中告诉我。

  • 我们将使用来自telegram.Bot 类的send_animation 方法。

    使用此方法发送动画文件(GIF 或 H.264/MPEG-4 AVC 视频无声)。

  • 我们需要知道我们希望发送的 GIF 的file_id重要提示:我们需要在the same chat with the bot 中获取file_id

以下是我们发送带有captioninline keyboard 的GIF 的方法(您可以在我的GitHub 上查看完整代码:wehavetogoback.py

keyboard = [
    [
        InlineKeyboardButton('yes ?', callback_data='yes'),
        InlineKeyboardButton('no ?', callback_data='no')
    ]
]

bot.send_animation(
    chat_id=update.message.chat.id,
    animation='file_id',
    caption='go back??',
    reply_markup=InlineKeyboardMarkup(keyboard)
)

【讨论】:

    【解决方案2】:

    使用这些方法:

    • update.message.send_animation()
    • bot.send_animation()
    • bot.edit_message_media()

    例如:

    def start(bot, update):
        gif_link='https://media.giphy.com/media/yFQ0ywscgobJK/giphy.gif'
        update.message.reply_animation(
            animation=gif_link,
            caption=main_menu_message(),
            reply_markup=main_menu_keyboard(),
            parse_mode=ParseMode.MARKDOWN
        )
    

    更新:@amir-a-shabani 感谢您的编辑,感谢@david-kha 使用代码示例)

    【讨论】:

    • 你明白了^_^一件小事;最后一行代码前面需要4个空格!我试图再次编辑它,但社区机器人拒绝了它。
    • 2 个空格!我向你的队友表示哀悼:))
    • 他们也使用 2 个空格)如果您可以在 @david 代码示例中看到,我认为这也是我的工作)
    • 我明白了,编码愉快?
    猜你喜欢
    • 2016-10-17
    • 2016-10-20
    • 2019-02-08
    • 1970-01-01
    • 2017-06-18
    • 2017-06-02
    • 2018-07-27
    • 2018-11-17
    • 1970-01-01
    相关资源
    最近更新 更多