【问题标题】:Django telegram bot reply keyboard not workingDjango电报机器人回复键盘不起作用
【发布时间】:2018-03-25 03:28:46
【问题描述】:

我有问题。我刚刚用 django-telegram-bot package 安装了 Django 我使用示例中的一个函数,它基本上向用户返回一条消息。我想要的是给他发送一个reply_keyboard。这很奇怪,但它不起作用。这是我的代码:

from telegram.ext import CommandHandler, MessageHandler, Filters
from telegram import ReplyKeyboardMarkup, InlineKeyboardMarkup, 
InlineKeyboardButton
from django_telegrambot.apps import DjangoTelegramBot

def me(bot, update):
    keyboard = [
    [
        InlineKeyboardButton("Option 1", callback_data='1'),
        InlineKeyboardButton("Option 2", callback_data='2')
    ],
    [   InlineKeyboardButton("Option 3", callback_data='3')
    ]
    ]

    reply_markup = InlineKeyboardMarkup(keyboard)

    # THIS IS PRINTING  TEXT BUT Not keyboard!
    update.message.reply_text('Please choose:', 
    reply_markup=reply_markup)

    # Again sent text
    bot.sendMessage(update.message.chat_id, text='text works' ,reply_markup=markup)

这个问题我已经坚持了 3 天了。

【问题讨论】:

  • 看来你在bot.sendMessage这一行写错了:reply_markup=markup - 没有初始化markup variable`

标签: django bots telegram


【解决方案1】:

这对我有用

def echo(bot, update):
    keyboard = [
        [
            InlineKeyboardButton("Option 1", callback_data='1'),
            InlineKeyboardButton("Option 2", callback_data='2')
        ],
        [InlineKeyboardButton("Option 3", callback_data='3')
         ]
    ]
    reply_markup = InlineKeyboardMarkup(keyboard)
    bot.sendMessage(update.message.chat_id, text=update.message.text,reply_markup=reply_markup)

看来你在reply_markup=markup 部分弄错了。

【讨论】:

  • 谢谢你,但我相信这不能解决已解释的问题。
  • @DmitryYudin 嗨,为什么?我已经测试了这段代码,它显示了一个 user_keyboard,你能告诉我你的代码有什么问题吗?
猜你喜欢
  • 2017-08-01
  • 2018-03-23
  • 2016-03-22
  • 2020-10-08
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2022-10-05
  • 1970-01-01
相关资源
最近更新 更多