1

I am newbie to Telegram Bot programming in Python. I created a simple bot @kawaikx_bot that has a /start command and can reply to any text input.

from telegram.ext import *
from datetime import datetime

weekdays = [0, 1, 2, 3, 4]
API_KEY = '********************************'


def start_command(update, context):
    name_of_day = datetime.today().weekday()
    if name_of_day in weekdays:
        reply = f"\U00002712 Its a weekday today"
        update.message.reply_text(reply, parse_mode='html')
    else:
        reply = f"&#x1F48E; <b>Its holiday</b>."
        update.message.reply_text(reply, parse_mode='html')
 


def handle_message(update, context):
    name_of_day = datetime.today().weekday()
    if name_of_day in weekdays:
        reply = f"\U00002712 Its a weekday today"
        update.message.reply_text(reply, parse_mode='html')
    else:
        reply = f"&#x1F48E; <b>Its holiday</b>."
        update.message.reply_text(reply, parse_mode='html')


def main():
    updater = Updater(API_KEY)
    dp = updater.dispatcher
    dp.add_handler(CommandHandler('start', start_command))
    dp.add_handler(MessageHandler(Filters.text, handle_message))
    updater.start_polling()
    updater.idle()

main()

I have enabled inline mode also. I am trying to pass a message to this bot from a group chat where bot is not a member by calling bot name and some text @kawaikx_bot hello. but it fails to send the reply.

I was expecting the reply 'Its a weekday today'

Can you help me figure out what's wrong with my code?

thanks in advance

KawaiKx
  • 9,558
  • 19
  • 72
  • 111

1 Answers1

1

CommandHandler and MessageHandler only catch updates that contain messages. Please have a look at this section of the official API docs as well as this PTB example. You should also look up the relevant classes & methods in the docs of PTB.


Disclaimer: I'm currently the maintainer of python-telegram-bot.

CallMeStag
  • 5,467
  • 1
  • 7
  • 22
  • thanks. It helped. I have another question. Package telegram.ext is superset of package telegram. am I right? I understood that learning package telegram.ext is enough for building telegram bots or do I need to learn package telegram too? excuse me if it is a stupid question but I have no one else to ask. thanks in advance – KawaiKx Nov 25 '21 at 07:34
  • 1
    the `telegram` package is a pure python implementation of the methods and types specified by the [official Bot API docs](https://core.telegram.org/bots/api). `telegram.ext` provides convenience functionality around that pure implementation which makes it easy to implement chat bots. e.g. comare [this example](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/rawapibot.py) to [this one](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/rawapibot.py) – CallMeStag Nov 25 '21 at 09:02