首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么telepot发送两条消息而不是一条消息?

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

Stack Overflow用户
提问于 2020-04-22 00:51:59
回答 1查看 66关注 0票数 1

我有一个问题,当telepot发送第二条消息时(按下‘reply_markup=keyboard_selection’键),telepot也发送第一条消息'bot.sendMessage(chat_id,text = "Cosa desideri fare?",reply_markup=keyboard_selection)‘

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-18 16:32:12

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

代码语言:javascript
复制
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。有了它,inline关键字的答案将转到您定义的回调函数,在那里您将只获得用户按下的文本。

下面是您的数据的示例:

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61348804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档