Python Telegram bot doesn't start running at all

Question:

I am a Python beginner trying to create a Telegram scheduler bot using the python-telegram-bot library and some code snippets I found online. However, when I try to run the code, the bot doesn’t seem to start running at all. I have double-checked that I am using the correct API token and that the bot is authorized to access my Telegram account.

Despite my efforts, I am not getting any error or exception messages. I have tried running the script on different machines, but the result is always the same.

Can anyone suggest what might be causing this issue? Are there any common mistakes I might have made as a beginner that could be preventing the bot from starting? How can I go about debugging the code to figure out what’s going wrong? Any help would be greatly appreciated!

import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from datetime import datetime, time

# replace YOUR_TOKEN_HERE with your bot's token
bot = telegram.Bot(token='[REMOVED BOT TOKEN BUT CAN CONFIRM IT'S CORRECT]')
updater = Updater(
    token='[REMOVED BOT TOKEN BUT CAN CONFIRM IT'S CORRECT]', use_context=True)
dispatcher = updater.dispatcher

    def start(update, context):
        context.bot.send_message(chat_id=update.effective_chat.id,
                                 text="Hi! I'm a bot that can help you schedule messages in your Telegram channel. To get started, please enter your message or attachment:")


start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)


def message_handler(update, context):
    context.user_data['message'] = update.message.text
    # or use update.message.photo or update.message.document to handle attachments
    context.bot.send_message(
        chat_id=update.effective_chat.id, text="Got it! Do you want to add a caption?")


message_handler_handler = MessageHandler(Filters.text, message_handler)
dispatcher.add_handler(message_handler_handler)


def caption_handler(update, context):
    context.user_data['caption'] = update.message.text
    context.bot.send_message(chat_id=update.effective_chat.id,
                             text="Great! Now, do you want to send the message every day at a specific time, or just once?")


caption_handler_handler = MessageHandler(Filters.text, caption_handler)
dispatcher.add_handler(caption_handler_handler)


def frequency_handler(update, context):
    if update.message.text.lower() == "once":
        # handle one-time message sending
        pass
    elif update.message.text.lower() == "recurring":
        context.user_data['recurring'] = True
        context.bot.send_message(chat_id=update.effective_chat.id,
                                 text="Got it! What time do you want to send the message every day? (format: HH:MM)")
        # add a message scheduler to send the message every day at the specified time
    else:
        context.bot.send_message(chat_id=update.effective_chat.id,
                                 text="I'm sorry, I didn't understand that. Please enter 'once' or 'recurring'.")
        return


frequency_handler_handler = MessageHandler(Filters.text, frequency_handler)
dispatcher.add_handler(frequency_handler_handler)


def time_handler(update, context):
    try:
        send_time = datetime.strptime(update.message.text, '%H:%M').time()
        context.user_data['send_time'] = send_time
        context.bot.send_message(chat_id=update.effective_chat.id,
                                 text="Thanks! Lastly, please enter the channel ID where you want to send the message.")
    except ValueError:
        context.bot.send_message(chat_id=update.effective_chat.id,
                                 text="Oops! That's an invalid time format. Please enter the time in HH:MM format.")
        return


time_handler_handler = MessageHandler(Filters.text, time_handler)
dispatcher.add_handler(time_handler_handler)


def channel_handler(update, context):
    context.user_data['channel_id'] = update.message.text
    context.bot.send_message(chat_id=update.effective_chat.id,
                             text="Got it! Your message has been scheduled to be sent.")
    # add code to schedule the message to be sent to the specified channel at the specified time


channel_handler_handler = MessageHandler(Filters.text, channel_handler)
dispatcher.add_handler(channel_handler_handler)


def view_messages(update, context):
    # add code to retrieve and display stored messages

    def edit_message(update, context):
        # add code to retrieve and edit a specific stored message

        view_messages_handler = CommandHandler('view_messages', view_messages)
        edit_message_handler = CommandHandler('edit_message', edit_message)
        dispatcher.add_handler(view_messages_handler)
        dispatcher.add_handler(edit_message_handler)

    updater.start_polling()

I have tried the following:

  • Double-checked that I am using the correct API token and that the bot is authorized to access my Telegram account.
  • Run the script on different machines to rule out any hardware-related issues.
  • Tried adding print() statements in various parts of my code to see if the script is executing at all.

Despite my efforts, the bot still doesn’t seem to start running. I am not getting any error or exception messages, so it’s difficult to pinpoint where exactly the issue might be.

Ideally, I was expecting to see the bot start running and to be able to interact with it via the Telegram app.

Asked By: mazahirrangwala

||

Answers:

You indentation looks off. Try this:

import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from datetime import datetime, time

# replace YOUR_TOKEN_HERE with your bot's token
bot = telegram.Bot(token="[REMOVED BOT TOKEN BUT CAN CONFIRM IT'S CORRECT]")
updater = Updater(
    token="[REMOVED BOT TOKEN BUT CAN CONFIRM IT'S CORRECT]", use_context=True)
dispatcher = updater.dispatcher

def start(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id,
                             text="Hi! I'm a bot that can help you schedule messages in your Telegram channel. To get started, please enter your message or attachment:")

start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)

# ... (keep the rest of your code the same)

def view_messages(update, context):
    # add code to retrieve and display stored messages
    pass

def edit_message(update, context):
    # add code to retrieve and edit a specific stored message
    pass

view_messages_handler = CommandHandler('view_messages', view_messages)
edit_message_handler = CommandHandler('edit_message', edit_message)
dispatcher.add_handler(view_messages_handler)
dispatcher.add_handler(edit_message_handler)

updater.start_polling()

In the code provided, updater.start_polling() will never execute, because it lives in a function body of a function that’s never called.

Answered By: Brock Brown