I am experiencing an issue with my Telegram bot, which fails to respond to any commands I send. The logs indicate that the webhook is set up correctly, and the bot itself is running, yet it doesn’t log any interactions or respond to messages. Can anyone assist in diagnosing the cause of this issue?
import telebot
from telebot.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery
import logging
from datetime import datetime, timezone
from flask import Flask, request, abort
# Configure logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.DEBUG
)
logger = logging.getLogger(__name__)
# Your bot token
TOKEN = "YOUR_BOT_TOKEN"
# Webhook URL
WEBHOOK_URL = "YOUR_WEBHOOK_URL"
# Initialize the bot
bot = telebot.TeleBot(TOKEN)
# Start command handler
@bot.message_handler(commands=['init'])
def welcome_user(message):
logger.info(f"/init command received from user {message.from_user.id}")
keyboard = InlineKeyboardMarkup()
keyboard.add(InlineKeyboardButton("Select time", callback_data='select_time'))
bot.send_message(
message.chat.id,
"Greetings! This bot will provide you with a word along with its meaning daily. Please select a convenient time for reading it.",
reply_markup=keyboard
)
# Button callback handler
@bot.callback_query_handler(func=lambda call: True)
def process_callback(call: CallbackQuery):
logger.info(f"Callback data received: {call.data}")
if call.data == 'select_time':
keyboard = InlineKeyboardMarkup()
keyboard.add(InlineKeyboardButton("Morning", callback_data='morning'))
keyboard.add(InlineKeyboardButton("Afternoon", callback_data='afternoon'))
keyboard.add(InlineKeyboardButton("Evening", callback_data='evening'))
keyboard.add(InlineKeyboardButton("Night", callback_data='night'))
bot.edit_message_text(
chat_id=call.message.chat.id,
message_id=call.message.message_id,
text="Select a time of day:",
reply_markup=keyboard
)
# Flask app to handle webhooks
app = Flask(__name__)
@app.route("/", methods=['POST'])
def handle_webhook():
logger.info("Webhook request received")
if request.headers.get('content-type') == 'application/json':
json_data = request.get_data().decode('utf-8')
logger.debug(f"JSON received: {json_data}")
update = telebot.types.Update.de_json(json_data)
bot.process_new_updates([update])
return 'OK', 200
else:
logger.warning("Invalid request header")
abort(400)
if __name__ == '__main__':
bot.remove_webhook()
bot.set_webhook(url=WEBHOOK_URL)
app.run(host='0.0.0.0', port=8443)