如何在 python-telegram-bot 中接收来自用户的消息?
How can I receive messages from users in python-telegram-bot?
我正在使用 python-telegram-bot
def delete(update: Update, context: CallbackContext) -> None:
"""delete account from database"""
num = random.randrange(111111,999999)
update.message.reply_text("Something to write here\n\n****" + str(num) + "****")
time.sleep(10)
if int(update.message.text) == num: #here update.message.text == '/cancel' and not the message user
flag = delete_db(update.effective_user.id)
if flag:
update.message.reply_text('OK')
else:
update.message.reply_text('Something goes wrong or time is out')
如何强制更新消息?我觉得是不是有问题...
我在电报社区的建议下解决了它,使用具有两个功能的会话处理程序,一个开始操作,第二个确认。
在 def main 中:
dispatcher.add_handler(
ConversationHandler(
entry_points = [MessageHandler(Filters.regex('^Cancel$'), delete)],
states = {
DELETE: [MessageHandler(Filters.text, random_number)],
},
fallbacks = [None], # here you can enter an /end function to break the process
conversation_timeout = 30,
),
)
启动函数'delete':
def delete(update: Update, context: CallbackContext):
update.message.reply_text('some message')
CallbackContext.chat_data = random.randrange(111111,999999)
update.message.reply_text("some other message\n*" + str(CallbackContext.chat_data) + "*")
return DELETE
保留字符串消息并与生成的随机数进行比较的函数:
def random_number(update: Update, context: CallbackContext):
try:
user_action = int(update.message.text)
if user_action == CallbackContext.chat_data:
#flag = delete_db(update.effective_user.id) # function to delete from database
if flag:
update.message.reply_text('Okay done')
else:
update.message.reply_text('Wrong number')
except:
update.message.reply_text('failed')
return ConversationHandler.END
我正在使用 python-telegram-bot
def delete(update: Update, context: CallbackContext) -> None:
"""delete account from database"""
num = random.randrange(111111,999999)
update.message.reply_text("Something to write here\n\n****" + str(num) + "****")
time.sleep(10)
if int(update.message.text) == num: #here update.message.text == '/cancel' and not the message user
flag = delete_db(update.effective_user.id)
if flag:
update.message.reply_text('OK')
else:
update.message.reply_text('Something goes wrong or time is out')
如何强制更新消息?我觉得是不是有问题...
我在电报社区的建议下解决了它,使用具有两个功能的会话处理程序,一个开始操作,第二个确认。
在 def main 中:
dispatcher.add_handler(
ConversationHandler(
entry_points = [MessageHandler(Filters.regex('^Cancel$'), delete)],
states = {
DELETE: [MessageHandler(Filters.text, random_number)],
},
fallbacks = [None], # here you can enter an /end function to break the process
conversation_timeout = 30,
),
)
启动函数'delete':
def delete(update: Update, context: CallbackContext):
update.message.reply_text('some message')
CallbackContext.chat_data = random.randrange(111111,999999)
update.message.reply_text("some other message\n*" + str(CallbackContext.chat_data) + "*")
return DELETE
保留字符串消息并与生成的随机数进行比较的函数:
def random_number(update: Update, context: CallbackContext):
try:
user_action = int(update.message.text)
if user_action == CallbackContext.chat_data:
#flag = delete_db(update.effective_user.id) # function to delete from database
if flag:
update.message.reply_text('Okay done')
else:
update.message.reply_text('Wrong number')
except:
update.message.reply_text('failed')
return ConversationHandler.END