简体   繁体   中英

Problems deploying to heroku

So this is my basic Twitter bot which uses Node to look for search terms from @ references and then find animated gifs associated with that:

//Dependencies
const Twitter = require('twitter');
const http = require('http');
const config = require('./config.js');
const giphyApiKey = (process.env.apiKey || config.giphy.apiKey);
let searchString = "";
let giphyQueryUrl;

//Initialize twitter client
const client = new Twitter({
  consumer_key: (process.env.consumer_key || config.twitter.consumer_key),
  consumer_secret: (process.env.consumer_secret || config.twitter.consumer_secret),
  access_token_key: (process.env.access_token_key || config.twitter.access_token_key),
  access_token_secret: (process.env.access_token_secret || config.twitter.access_token_secret)
});

process.on('unhandledRejection', (reason,promise) => {
    console.log('Unhandled Rejection at:', promise, 'reason:', reason);
})

//This stream checks for statuses containing '@[USERNAME]' references, strips out the relevant seach data and feeds
//that search data to the queryGiphy function
client.stream('statuses/filter', {track: '@[USERNAME]'}, (stream) => {
    stream.on('data', (tweet) => {
        searchString  = 
            tweet.text.substring(tweet.text.indexOf("@[USERNAME]")+"@[USERNAME]".length + 1,tweet.text.length);
        giphyQueryUrl = "http://api.giphy.com/v1/gifs/search?q="+searchString+"&api_key="+ giphyApiKey +"&limit=5&rating=g";
        let replyString = '@' + tweet.user.screen_name + ' ';
        let giphyUrl = queryGiphy(replyString,giphyQueryUrl);
    })
})

//This function will query the Giphy API using the node http module and when it finds a match, posts that to twitter
//using the gifUrlToPost function
function queryGiphy(replyString,queryUrl) {
    http.get(queryUrl, res => {
        res.setEncoding("utf8");
        let body = '';
        res.on("data", data => {
            body += data;
        });
        res.on("end", () => {
            body = JSON.parse(body);
            if(postToTwitter(replyString + body.data[0].url)) {
                return true;
            } else {
                return false;
            }

        });
        res.on("error", error => {
            console.log("Error: " + error);
        })

    });

}

//This simply posts the url of the gif passed to it
function postToTwitter(body) {
    client.post('statuses/update', {status: body})
        .then(tweet => {
            return true;
        })
        .catch(error => {
            throw error;
        });

}

My problem is that when I deploy this to heroku, I keep getting a timeout error that there is no place to connect $PORT to for heroku to dynamically assign a port. Now, I understand the nature of this, I've deployed small web servers to heroku before and understood how to set up environmental variables so that it can dynamically assign a port number for setting up a stream with Twitter and querying Giphy. But where do I do it in my code? I don't understand, in this case, where to do it?

do you have app.js file or the bin file if you do have then

app.set('port',(process.env.PORT||5000));

app.listen(app.get('port'),function(){

})

or you need to create a Procfile in your root and push the code to heroku git

you may read this article for futher clarification https://devcenter.heroku.com/articles/procfile

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