Merge pull request #3 from derogab/dev

Fix cron worker
This commit is contained in:
Gabriele De Rosa 2021-05-21 18:25:03 +02:00 committed by GitHub
commit 0ac692622d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 7 deletions

17
bot.py
View File

@ -339,7 +339,7 @@ def job():
print('Job running...') print('Job running...')
# Download data # Download data
data = download() intro, plot1, plot2 = download()
# Get active user with daily news # Get active user with daily news
users = db.users.find({ users = db.users.find({
@ -354,8 +354,16 @@ def job():
for user in users: for user in users:
try: try:
# Send photo # Send photos
updater.bot.send_photo(chat_id=user['_id'], photo=open(data['results'], 'rb'), caption="") p1 = open(intro['results'], 'rb')
p2 = open(plot1['results'], 'rb')
p3 = open(plot2['results'], 'rb')
p1 = InputMediaPhoto(media=p1)
p2 = InputMediaPhoto(media=p2)
p3 = InputMediaPhoto(media=p3)
updater.bot.send_media_group(chat_id=user['_id'], media=[p1, p2, p3])
# Count news successfully sent # Count news successfully sent
news_sent_counter = news_sent_counter + 1 news_sent_counter = news_sent_counter + 1
# Log # Log
@ -370,6 +378,7 @@ def job():
print("[JOB] Results: {sent} messages sent, {fail} messages failed.".format(sent=news_sent_counter, fail=news_fail_counter)) print("[JOB] Results: {sent} messages sent, {fail} messages failed.".format(sent=news_sent_counter, fail=news_fail_counter))
# Remove tmp files # Remove tmp files
for data in [intro, plot1, plot2]:
os.remove(data['plot']) os.remove(data['plot'])
os.remove(data['webpage']) os.remove(data['webpage'])
os.remove(data['results']) os.remove(data['results'])
@ -401,7 +410,7 @@ start_handler = CommandHandler('news', news)
dispatcher.add_handler(start_handler) dispatcher.add_handler(start_handler)
# Setup cron # Setup cron
schedule.every().day.at("18:00").do(job) schedule.every().day.at("18:30").do(job)
# Start bot # Start bot
updater.start_polling() updater.start_polling()