【问题标题】:'NoneType' object has no attribute 'chat_id' telegram bot“NoneType”对象没有属性“chat_id”电报机器人
【发布时间】:2021-08-15 18:28:57
【问题描述】:

当我点击 MESSAGE 按钮时,我得到了'NoneType' object has no attribute 'chat_id'。 我认为问题是 context=update.message.chat_id (context.job_queue.run_daily - 方法消息)。 另一个问题是我在单击时收到消息,而不是在 12:00(我放入方法 run_daily 的时间)

def start(update:Update, context:CallbackContext) -> int:
                
    options = [[InlineKeyboardButton("MESSAGE", callback_data="DAILY_MESSAGE")]]
                        
    optionsMenu = InlineKeyboardMarkup(options)
                        
    update.message.reply_text("Welcome", reply_markup=optionsMenu)
                        
    return FIRST
    
    
def messageText(update, context):
    context.bot.send_message(
            chat_id=update.callback_query.from_user.id, 
            text="Message received",
            parse_mode=ParseMode.MARKDOWN
            )
            
def message(update, context):
    context.job_queue.run_daily(messageText(update, context),
                                time=time(hour=12, minute=0, second=0, tzinfo=pytz.timezone("Europe/Madrid")),
                                days=(0, 1, 2, 3, 4, 5, 6),
                                context=update.message.chat_id)
    
    
def main():
    updater = Updater("API_KEY", use_context=True)
    dp = updater.dispatcher
    
    
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            FIRST: [
                CallbackQueryHandler(message, pattern='^DAILY_MESSAGE$')
            ],
        },
        fallbacks=[CommandHandler('start', start)],
    )
    
    dp.add_handler(conv_handler)
        
    updater.start_polling()
    updater.idle()

有什么解决办法吗?谢谢

【问题讨论】:

标签: python python-3.x telegram telegram-bot python-telegram-bot


【解决方案1】:

关于AttributeError,请参阅我在 cmets 中链接的问题。关于I get the message when I click, not at 12:00

job_queue.run_*需要知道在指定时间运行哪个函数。在您的情况下,它应该运行messageText。但不是传递该函数,而是传递messageText(update, context)。这意味着您已经通过传递参数调用了messageText,然后将该调用的返回值传递给job_queue.run_daily。另请注意,messageText 只能采用一个位置参数,即context,因为更新不会触发作业。

请阅读 PTB wiki 中的tutorial on JobQueue


免责声明:我目前是python-telegram-bot 的维护者。

【讨论】:

  • 我已经更改了方法并且它可以工作,但前提是我使用它创建了一个 CommandHandler,但我不希望这样。我希望该方法在我单击按钮时起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
  • 2021-11-02
相关资源
最近更新 更多