Python 为什么telepot发送两条消息而不是一条?

Python 为什么telepot发送两条消息而不是一条?,python,telegram,telepot,Python,Telegram,Telepot,我有一个问题,当telepot发送第二条消息时(按“回复标记=键盘选择”后),telepot还会发送第一条消息“bot.sendMessage(chat\u id,text=“Cosa desideri fare?”,回复标记=键盘选择)” 我想一个简单的解决方法是: def handle(msg): content_type, chat_type, chat_id = telepot.glance(msg) text = msg['text'] if text == "

我有一个问题,当telepot发送第二条消息时(按“回复标记=键盘选择”后),telepot还会发送第一条消息“bot.sendMessage(chat\u id,text=“Cosa desideri fare?”,回复标记=键盘选择)”


我想一个简单的解决方法是:

def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    text = msg['text']
    if text == "Cerca fermata ora":
        bot.sendMessage(chat_id, text = "Cosa", reply_markup=keyboard_selection)
    else:
        bot.sendMessage(chat_id, text = "Cosa desideri fare?", reply_markup=keyboard_selection)
此外,如果您想以一种有序的方式进行操作,我建议您使用
InlineKeyboardMarkup
。使用它,内联关键字的答案将转到您定义的回调函数,在那里您将只获得用户按下的文本

您的数据示例如下:

import time
import telepot
from telepot.loop import MessageLoop
import telepot.namedtuple
bot = telepot.Bot("1210935912:AAG4X8vHlXLM3jQWnxFKDB2NsZ6pqTQM7lQ")
lista = ["New York","Los Angeles","Miami","Toronto","Berlin","Rome","Ciao"]
seq = iter(lista)

keyboard = InlineKeyboardMarkup(inline_keyboard=[
                                [InlineKeyboardButton(text='Cerca fermata ora', callback_data='cerca')],
                                [InlineKeyboardButton(text='Pianififca Viaggio', callback_data='planifica')]
                                                ]
                                )
def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    bot.sendMessage(chat_id, text = "Cosa desideri fare?", reply_markup=keyboard)

def on_callback_query(msg):        
    query_id, chat_id, query_data = telepot.glance(msg, flavor='callback_query')

    if query_data == "cerca":
        bot.sendMessage(chat_id, text = "Cosa cerca", parse_mode='markdown')
    if query_data == "planifica":
        bot.sendMessage(chat_id, text = "Cosa planifica", parse_mode='markdown')

MessageLoop(bot, {'chat' : handle,
                  'callback_query' : on_callback_query}).run_as_thread()

我想一个简单的解决方法是:

def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    text = msg['text']
    if text == "Cerca fermata ora":
        bot.sendMessage(chat_id, text = "Cosa", reply_markup=keyboard_selection)
    else:
        bot.sendMessage(chat_id, text = "Cosa desideri fare?", reply_markup=keyboard_selection)
此外,如果您想以一种有序的方式进行操作,我建议您使用
InlineKeyboardMarkup
。使用它,内联关键字的答案将转到您定义的回调函数,在那里您将只获得用户按下的文本

您的数据示例如下:

import time
import telepot
from telepot.loop import MessageLoop
import telepot.namedtuple
bot = telepot.Bot("1210935912:AAG4X8vHlXLM3jQWnxFKDB2NsZ6pqTQM7lQ")
lista = ["New York","Los Angeles","Miami","Toronto","Berlin","Rome","Ciao"]
seq = iter(lista)

keyboard = InlineKeyboardMarkup(inline_keyboard=[
                                [InlineKeyboardButton(text='Cerca fermata ora', callback_data='cerca')],
                                [InlineKeyboardButton(text='Pianififca Viaggio', callback_data='planifica')]
                                                ]
                                )
def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    bot.sendMessage(chat_id, text = "Cosa desideri fare?", reply_markup=keyboard)

def on_callback_query(msg):        
    query_id, chat_id, query_data = telepot.glance(msg, flavor='callback_query')

    if query_data == "cerca":
        bot.sendMessage(chat_id, text = "Cosa cerca", parse_mode='markdown')
    if query_data == "planifica":
        bot.sendMessage(chat_id, text = "Cosa planifica", parse_mode='markdown')

MessageLoop(bot, {'chat' : handle,
                  'callback_query' : on_callback_query}).run_as_thread()

您不应在问题中包含您的机器人令牌!但是,每次调用
handle
时,您都会调用
sendMessage
,因此每次都会包含该消息。在发送
Cosa desideri fare?
消息之前,您可能需要检查
text
。谢谢,我将更改我的机器人令牌,我忘了。是的,要检查文本。那么,我如何解决我的问题?在发送第一条消息之前移动
文本
比较,并且仅在文本与第二步不匹配时发送消息。您不应在问题中包含您的bot令牌!但是,每次调用
handle
时,您都会调用
sendMessage
,因此每次都会包含该消息。在发送
Cosa desideri fare?
消息之前,您可能需要检查
text
。谢谢,我将更改我的机器人令牌,我忘了。是的,要检查文本。那么,我该如何解决我的问题呢?在发送第一条消息之前,移动
文本
比较,并且仅在文本与第二步不匹配时发送消息。