0

When trying to sendMessage it tells me the chat id is not defined. I am able to answerCallbackQuery because it needs query ID not chat. If I try to enter in 'chat_id' in the DEF on callback query area it throws up more errors

Where exactly in the code do I need to define it?

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

def on_chat_message(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    #creating buttons
    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)


    #find callback data
    if query_data == 'a':
    #bot.sendMessage(chat_id, 'dsuhsdd')
    #answerCallbackQuery puts the message at top
    bot.answerCallbackQuery(query_id, 'products')
        
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)            
Shaido
  • 27,497
  • 23
  • 70
  • 73
ible
  • 55
  • 6

1 Answers1

1

The chat_id variable is local to on_chat_message. on_callback_query does not have access to it. The most Pythonic method would make these into a class and store the chat id in a member variable, but you can do it here by adding

    global chat_id

as the first line of on_chat_message. You don't need that in on_callback_query because you aren't changing the value, although it won't hurt.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30