1

I have created inline keyboard and am trying to answer the callback query with a message. The terminal is recieving the query but i do not know the correct syntax for replying to it with messages and ultimately photos and other things.

I sometimes get a telegram error 400. After googling they said i should confirm my token in conf.py and i have done that

import sys
import time
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
from telepot.delegate import (
   per_chat_id, create_open, pave_event_space, include_callback_query_chat_id)

def on_chat_message(msg):
   content_type, chat_type, chat_id = telepot.glance(msg)

   if content_type == 'text':
       if msg['text'] == '/start':
          bot.sendMessage(chat_id, 'Welcome to @UK_Cali Teleshop\n      Created by JonSnow 2021',reply_markup = InlineKeyboardMarkup(inline_keyboard=[
                                   [InlineKeyboardButton(text="Feedback",callback_data='a'), InlineKeyboardButton(text="You",callback_data='b'),InlineKeyboardButton(text="PGP",callback_data='c'), InlineKeyboardButton(text="Cunt",callback_data='d')],
                                   [InlineKeyboardButton(text="Products",callback_data='e')]
                               ]
                           ))

def on_callback_query(msg):
   query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
   print('Callback Query:', query_id, from_id, query_data)

   if query_data == 'a':
       bot.answerCallbackQuery(query_id, text='Welcome to @UK_Cali Teleshop')



   

bot = telepot.Bot('1646167995:AAGsOwfjcryYYkoah69QJ6XGA7koUywmuRk')
MessageLoop(bot, {'chat': on_chat_message,
                 'callback_query': on_callback_query}).run_as_thread()
print('Listening ...')

while 1:
   time.sleep(10)            

Leonardo Scotti
  • 1,069
  • 8
  • 21
ible
  • 55
  • 6
  • 1
    if you want to send message, photos, audios, videos or documents use:`bot.SendMessage`, `bot.sendPhoto`, `bot.sendDocument`, `bot.sendAudio`, `bot.sendVideo`, these are all telepot functions, please for more info read the docs [here](https://telepot.readthedocs.io/en/latest/) – Leonardo Scotti Mar 01 '21 at 15:28
  • I am trying to send them as a reply to a callback query – ible Mar 01 '21 at 15:54
  • 1
    use the above function in your if elese in the OnCallbackQuesy function – Leonardo Scotti Mar 01 '21 at 15:55
  • can you give me an example of how that might look please – ible Mar 01 '21 at 15:56

1 Answers1

0

to reply to a callback query with a msg, photo, audio, video document you have to do:

import time
import telepot
from telepot.loop import MessageLoop
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton

TOKEN = "super secret bot token"

def on_chat_message(msg):

    #here you handel messages and create the iniline keyboard

    content_type, chat_type, chat_id = telepot.glance(msg)

    keyboard = InlineKeyboardMarkup(inline_keyboard=[
                   [InlineKeyboardButton(text='button text', callback_data='callback query data for reconizing it')],
               ])


def on_callback_query(msg):

    #here you handels callback querys,
    #the event that are fired when user clickan inline keyboard'sbutton


    query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')

    #do something  HERE based on the callback query to reply,
    #to recognize the button pressed check query_data,
    #that corresponds to he callback_data setted when creating the button


    #to send text messages:
    bot.sendMessage(chat_id, "your test message")

    #to send photo:
    bot.sendPhoto(chat_id, "C:/path/to/your/photo or https://link.to/your/photo")

    #to send video:
    bot.sendPhoto(chat_id, "C:/path/to/your/video or https://link.to/your/video")

    #to send audio:
    bot.sendPhoto(chat_id, "C:/path/to/your/audio or https://link.to/your/audio")

    #to send document:
    bot.sendPhoto(chat_id, "C:/path/to/your/document or https://link.to/your/document")

    #please note that you can do the exactly above thing 
    #in the on_chat_message to reply to a txt message
    #ans that in any case you can use if...else statement to make the 
    #reply different by the msg/inline button pressed


bot = telepot.Bot(TOKEN)
MessageLoop(bot, {'chat': on_chat_message, 
                  'callback_query': on_callback_query}).run_as_thread()

while True:
    time.sleep(10)
Leonardo Scotti
  • 1,069
  • 8
  • 21