Message from a telegram bot without a command(python)

Viewed 485

I want to send a Message(call a function) every day at a given Time. Sadly this is not possible with message.reply_text('Test'). Is there any way i can do this? I could not find anything.

This is my current code:

import telegram.ext
from telegram.ext import CommandHandler, MessageHandler, Filters
import schedule
import time

API_KEY = 'XXXXXXXXXXX'   
updater = telegram.ext.Updater(API_KEY)
dispatcher = updater.dispatcher


def start(update, context):
    update.message.reply_text('Welcome!')


# problem:
def Test(update, context):
    update.message.reply_text('Works!!!')


# running special functions every Day at a given Time
schedule.every().day.at("10:00").do(Test)


while True:
    schedule.run_pending()
    time.sleep(1)


def main():
    # add handlers for start and help commands
    dispatcher.add_handler(CommandHandler("start", start))


    # start your bot
    updater.start_polling()
    # run the bot until Ctrl-C
    updater.idle()

The schedule part works, I just don`t know how to send this Message. Thanks for your help!

1 Answers

Update object, inside of the message field, has the from field which is a User Telegram object containing the user's ID.

Once you have the user's ID, you can use the sendMessage method in order to reply him easily.

To conclude, instead of:

update.message.reply_text('Welcome!')

You could do like so:

user_id = update.message.from.id
updater.sendmessage(chat_id=user_id, text="Welcome!")
Related