简体   繁体   中英

Twitter bot stops after first post after being deployed on Heroku

I made a simple little twitter bot that quote tweets itself once an hour, and for some reason it stops working after it makes the first tweet. These are the heroku logs below:

https://imgur.com/MN2SYOs

Looks like the "state" just switches from "up" to "down," but it doesn't say why. I know my code works because I tested it a lot locally and it worked just fine, so I'm not sure what's happening here. Here's the main code for the bot:

import tweepy
import time
import sys
import os

#--------------------Twitter credentials---------------------#
from os import environ

#Fill in on Heroku dashboard
CONSUMER_KEY = environ['CONSUMER_KEY']
CONSUMER_SECRET = environ['CONSUMER_SECRET']
ACCESS_KEY = environ['ACCESS_KEY']
ACCESS_SECRET = environ['ACCESS_SECRET']

#---------------------Connect to Twitter---------------------#
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

#----------------------Generate Tweets-----------------------#
lastTweet = None
num = 1
def createTweet():
    global lastTweet
    global num

    #Get most recent tweet ID
    for status in api.user_timeline('RecursionBot', count = 1):
        tweetID = status.id

    #Attach ID to template URL
    blankURL = 'https://twitter.com/RecursionBot/status/'
    mostRecentTweet = "{}{}".format(blankURL, tweetID)

    #Generate new tweet
    tweet = "Level: {} \n {}".format(num, mostRecentTweet)
    num += 1

    return tweet

#------------------------Post Timer-------------------------#
interval = 60 * 60
while True:
    print("Generating tweet...")
    newTweet = createTweet()
    api.update_status(newTweet)
    time.sleep(interval)

Am I missing something?

Apps on Heroku that do not do anything within an hour will be idled to save compute resource. You can read more about this on the Heroku site.

When Do Apps Sleep?

When an app on Heroku has only one web dyno and that dyno doesn't receive any traffic in 1 hour, the dyno goes to sleep.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM