为什么 telepot 发送两条消息而不是一条?
Why does telepot send two message instead one?
我有一个问题,当 telepot 发送第二条消息时(按下 'reply_markup=keyboard_selection' 之后),telepot 也会发送第一条消息 'bot.sendMessage(chat_id, text = "Cosa desideri fare?", reply_markup=keyboard_selection)'
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)
#reqloc = keyboard = {"text": "Utilizza la geolocalizzazione", "request_location": True} Update Beta 1.01
#keyboard = {"keyboard": [[reqloc]]+[[{"text": i} for i in pair] for pair in zip(seq)]}
keyboard_locpo = {"keyboard": [[{"text": i} for i in pair] for pair in zip(seq)]}
keyboard_selection = {"keyboard": [[{"text": "Cerca fermata ora"}],
[{"text": "Pianififca Viaggio"}]]}
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
bot.sendMessage(chat_id, text = "Cosa desideri fare?", reply_markup=keyboard_selection)
text = msg['text']
if text == "Cerca fermata ora":
bot.sendMessage(chat_id, text = "Cosa", reply_markup=keyboard_selection)
我想一个简单的解决方法是:
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()
我有一个问题,当 telepot 发送第二条消息时(按下 'reply_markup=keyboard_selection' 之后),telepot 也会发送第一条消息 'bot.sendMessage(chat_id, text = "Cosa desideri fare?", reply_markup=keyboard_selection)'
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)
#reqloc = keyboard = {"text": "Utilizza la geolocalizzazione", "request_location": True} Update Beta 1.01
#keyboard = {"keyboard": [[reqloc]]+[[{"text": i} for i in pair] for pair in zip(seq)]}
keyboard_locpo = {"keyboard": [[{"text": i} for i in pair] for pair in zip(seq)]}
keyboard_selection = {"keyboard": [[{"text": "Cerca fermata ora"}],
[{"text": "Pianififca Viaggio"}]]}
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
bot.sendMessage(chat_id, text = "Cosa desideri fare?", reply_markup=keyboard_selection)
text = msg['text']
if text == "Cerca fermata ora":
bot.sendMessage(chat_id, text = "Cosa", reply_markup=keyboard_selection)
我想一个简单的解决方法是:
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()