1

i have a problem, when telepot sends the second message (after pressing the 'reply_markup=keyboard_selection'), telepot sends also the first message '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)
Abdul Gandal
  • 97
  • 1
  • 7
  • 1
    You should never include your bot token in the question.. [Time to regenerate it](https://core.telegram.org/bots#6-botfather)! But seeing as you're calling `sendMessage` each time `handle` is called, the message will be included each time. You probably want to check `text` _before_ sending the `Cosa desideri fare?` message. – MatsLindh Apr 21 '20 at 17:30
  • Thank you, i will change my bot token, i've forgot it. Yes, want to check text. – Abdul Gandal Apr 21 '20 at 17:33
  • So, how can i solve my problem? – Abdul Gandal Apr 21 '20 at 17:34
  • Move the `text` comparison before sending the first message, and only send the message if the text doesn't match what you'd want for the second step. – MatsLindh Apr 21 '20 at 17:34

1 Answers1

0

I guess that an easy fix is:

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)

Moreover if you wanna do it in a much ordered way I will suggest to use the InlineKeyboardMarkup. With it the answer of the inline keyword will go to a callback function you define and there you will only obtain the text with that the user is pressing.

An example with your data will be:

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()
IMB
  • 519
  • 4
  • 19