Search code examples
pythonbotstelegram

Method to send messages to a channel using telegram bot API, python 3 and JobQueue


Basically I'm trying to understand how this API works, by doing the following .I'm not able to get the message to my channel (I'm sending the /test command from my telegram account). Also, how I'm supposed to use JobQueue and send auto-msgs every 3 hours for example? The documentation explanation doesn't works for me.

def test(bot, update):
    update.send_message(chat_id='@channelid', text='this is a test')

def main():
    # Create the EventHandler and pass it your bot's token.
    updater = Updater("457160310:AAFlxrH2uAaOMGrgO0suOXFM2gVKywsUL0E")
    dp = updater.dispatcher

    dp.add_handler(CommandHandler("test", test))

    dp.add_error_handler(error)

    updater.start_polling()

    updater.idle()


if __name__ == '__main__':
    main()

Solution

  • For the job implementation to send messages on intervals you can read this page that is full of examples. Someone should be the very same thing you want to do.

    About the command thing, I think that the library doesn’t get commands from channels so you may consider to:

    • send the command in private chat to the bot and the bot sends the reply to the channel (better thing in my opinion)

    • handle messages from channels (not commands) and checking if the text of the message contains the command

    I would suggest the first solution so you can even keep clean the channel