3

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?

faeze saghafi
  • 650
  • 10
  • 25

1 Answers1

1

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()
dzNET
  • 930
  • 1
  • 9
  • 14
  • sorry about question but for YOUR_TELEGRAM_ID , can set bot.getMe().get('id') ? – faeze saghafi Sep 12 '17 at 02:58
  • Nope. `bot.getMe()` return bot credentials. to get your own ID use `bot.getUpdates()` shuld be like `message.chat.id` http://telepot.readthedocs.io/en/latest/#receive-messages your code example printing it by each message to him. in this row `print(content_type,chat_type,chat_id)` chat_id - your ID in Telegram. – dzNET Sep 12 '17 at 03:54
  • I hope that more clearly, despite my bad English. Happy coding. – dzNET Sep 12 '17 at 04:21