python - telegram bot sendMessage in specific date

2.7k views Asked by At

I am terribly new in python and my progress is like a snail:( I want to make a telegram bot that send a message at specific date and time. I used apscheduler and telepot libraries for that. and this is my code:

import telepot
import sys
import time
from time import sleep
from datetime import datetime
from apscheduler.scheduler import Scheduler
import logging


bot = telepot.Bot("***")
logging.basicConfig()
sched = Scheduler()
sched.start()
exec_date = datetime(2017, 9, 12 ,1,51,0)


def handle(msg):
    content_type,chat_type,chat_id = telepot.glance(msg)
    print(content_type,chat_type,chat_id)

    if content_type == 'text' :
        bot.sendMessage(chat_id,msg['text'])


def sendSimpleText():
#     content_type,chat_type,chat_id = telepot.glance(msg)
#     print(content_type,chat_type,chat_id)
#     
#     if content_type == 'text' :
    chat_id = telepot.    
    bot.sendMessage(chat_id,'faez')


def main():
    job = sched.add_date_job(sendSimpleText, exec_date)
    while True:
        sleep(1)
        sys.stdout.write('.'); sys.stdout.flush()
#     bot.message_loop(handle)
# #     job = sched.add_date_job(sendSimpleText, '2017-09-11 21:35:00', ['testFuckBot'])
#     while True:
#         time.sleep(10)

if __name__ == '__main__':
    main()

my question is what do I pass to sendSimpleText as argument in add_date_job? in this line:

job = sched.add_date_job(sendSimpleText, exec_date)

I know that msg is the message that user is typed so for add_date_job I have nothing?

1

There are 1 answers

3
dzNET On BEST ANSWER

You are used an old (2.1.2) version of APScheduler. New version has a new syntax.

A function add_date_job no more available. This is a worked solution for you:

import telepot
import sys
import time
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from telepot.loop import MessageLoop
import logging

bot = telepot.Bot("***YOUR_BOT_TOKEN***")
logging.basicConfig()
sched = BackgroundScheduler()
exec_date = datetime(2017, 9, 12 ,3,5,0)

def handle(msg):
    content_type,chat_type,chat_id = telepot.glance(msg)
    print(content_type,chat_type,chat_id)

    if content_type == 'text' :
        bot.sendMessage(chat_id,msg['text'])


def sendSimpleText(chat_id):
    bot.sendMessage(chat_id,'faez')


def main():
    MessageLoop(bot, handle).run_as_thread()
    job = sched.add_job(sendSimpleText, run_date=exec_date, args=['**YOUR_TELEGRAM_ID**'])

    while True:
        time.sleep(1)
        sys.stdout.write('.'); sys.stdout.flush()

if __name__ == '__main__':
    sched.start()
    main()