1

I'm trying to send a message after a user has clicked on the inline buttons. It tells me the chat_id is not valid. When i take it out it tells me i have to include it.

Every other button works fine and i recieve the callback in terminal

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.sendMessage(chat_id, 'dsuhsdd')
        
ible
  • 55
  • 6
  • 3
    Is the chat_id a global variable? The context of the snippet you posted does not include where the chat_id is comming from. – collinsuz Mar 01 '21 at 18:50
  • The error is that the global name is not defined – ible Mar 01 '21 at 18:54
  • 1
    Python would not allow you use a variable without throwing a **reference error**. You have to check your source code (do a find with your code editor) to see where the variable **chat_id** is defined. It's probably an empty string or a nonsensical value. – collinsuz Mar 01 '21 at 19:10
  • I'll have to open a new question when i get a chance – ible Mar 01 '21 at 19:14

2 Answers2

0

Good, I see the problem, First get an authorization token >> https://telepot.readthedocs.io/en/latest/#get-a-token

Next, add this to get the chat_id

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

You were using query_data as a reference to chat_id

collinsuz
  • 439
  • 1
  • 4
  • 13
0

you don't need to comlicate your life with global scoping, because in your on_callback_query function you glanced the query so you retrieved 3 var:

query_id, from_id, query_data = telepot.glance(msg)

so you only need to use from_id that represents the chat_id that sended the query, so:

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.sendMessage(from_id, 'dsuhsdd')

when you glance a message you have:

content_type, chat_type, chat_id = telepot.glance(msg)
#type of mesg, type of chat, chat identifier

but when you glance a callback query:

query_id, from_id, query_data = telepot.glance(msg)
#query id, chat_id, txt data related to the query 
Leonardo Scotti
  • 1,069
  • 8
  • 21