1
votes

I need to send telegram notifications to users. So I am using python-telegram-bot library for the same.

Now to send notifcations to the user I have created following method :

def send_notification(chat_id,text) :
    try :
        if chat_id is None or text is None :
            raise Exception("Invalid parameters")
        print(BOT)
        BOT.send_message(chat_id=chat_id, text=text,parse_mode="HTML")
    except Exception :
        logger.exception("AN exception occurred while sending telegram notification")


# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(bot, update):
    """Send a message when the command /start is issued."""
    get_chat_id(update.message)
    BOT = bot
    bot.send_message(chat_id=update.message.chat_id, text="Subscribed successfully to  Bot")

Now the problem is I am not able to get the bot object. If I try to initialize one again by creating an object of class telegram.Bot I get the following exception :

[2019-07-18 10:52:56,517: ERROR/ForkPoolWorker-7] Error while getting Updates: Conflict: terminated by other getUpdates request; make sure that only one bot instance is running (409)

My bot is running using worker queues in celery. All I need is to get the instance of bot so that I can send message, I already have the chat id of the user saved in the database when start command is issued by the user. Can someone help me with this?

1
you're running 2 instances of your bot simultaneously - Ivan Vinogradov
My initial questiion is that how do I get an instance of bot class. The error ocurred when I forecfully created another. - Paras

1 Answers

0
votes

You should have users in txt or json file and then use this:

def send_notification(text) :
  with open(file_path, "r") as jsonFile:
    data = json.load(jsonFile)
    for user in data:
      user_id = data[user]['id']
      try:
        bot.send_message(user_id,text)
      except:
        pass