如何使用python-telegram-bot按下启动时使用内联键盘发送gif?

问题描述 投票:1回答:2

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

我使用了来自.inputmedia.documenthere的一些变体。

我一无所知,没有蟒蛇知识。我只能通过互联网上的阅读来理解。我真的无法理解如何表达它。

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
2个回答
0
投票

你的意思是这样的?

enter image description here

如果是,请继续阅读!如果不是,请在评论中告诉我。

  • 我们将使用send_animation类的telegram.Bot方法。 使用此方法发送动画文件(没有声音的GIF或H.264 / MPEG-4 AVC视频)。
  • 我们需要知道我们希望发送的GIF的file_id!重要提示:我们需要在file_id获得the same chat with the bot

以下是我们如何使用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)
)

0
投票

使用这些方法:

  • 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
    )

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

© www.soinside.com 2019 - 2024. All rights reserved.