title: Node Twitter bot keeps crashing on Heroku
body: <p>As a way to try to teach myself a bit of Node and how to use Heroku, I decided to create a simple Twitter bot that should tweet once a day. </p>
  
  <p>When running my bot manually, everything goes fine and it tweets as it should. Once on Heroku, even before I added a scheduler (to run my bot once a day), it started tweeting at random times. I have to keep deleting them. I noticed in the logs, the bot keeps crashing and restarting, which triggers the tweet. </p>
  
  <p>I have Googled for hours and cannot figure out why this is happening. I'm brand new to Node and anything to do with servers in general, so I'm really not sure how to troubleshoot from this point. </p>
  
  <p>I'd appreciate any help in figuring out what is causing my bot to crash.</p>
  
  <p>(I should note, I do have it set up as a <code>worker</code> on Heroku, and that is in my <code>Procfile</code>. Not sure if this makes a difference.)</p>
  
  <p><strong>Heroku Logs</strong>:</p>
  
  <pre><code>    2018-07-08T16:21:10.427313+00:00 heroku[worker.1]: Starting process with command `node bot.js`
      2018-07-08T16:21:11.167238+00:00 heroku[worker.1]: State changed from starting to up
      2018-07-08T16:21:15.159855+00:00 heroku[worker.1]: State changed from up to crashed
      2018-07-08T16:21:15.056356+00:00 heroku[worker.1]: Process exited with status 0
      2018-07-08T16:56:32.097321+00:00 heroku[worker.1]: State changed from crashed to starting
      2018-07-08T16:56:36.064795+00:00 heroku[worker.1]: Starting process with command `node bot.js`
      2018-07-08T16:56:36.789264+00:00 heroku[worker.1]: State changed from starting to up
      2018-07-08T16:56:40.212853+00:00 heroku[worker.1]: State changed from up to crashed
      2018-07-08T16:56:40.170153+00:00 heroku[worker.1]: Process exited with status 0
      2018-07-08T17:08:08.000000+00:00 app[api]: Build started by user myemailaddress
      2018-07-08T17:08:23.727988+00:00 app[api]: Deploy 852c8541 by user myemailaddress
      2018-07-08T17:08:23.727988+00:00 app[api]: Release v17 created by user myemailaddress
      2018-07-08T17:08:24.186005+00:00 heroku[worker.1]: State changed from crashed to starting
      2018-07-08T17:08:24.000000+00:00 app[api]: Build succeeded
      2018-07-08T17:08:26.514939+00:00 heroku[worker.1]: Starting process with command `node bot.js`
      2018-07-08T17:08:27.156637+00:00 heroku[worker.1]: State changed from starting to up
      2018-07-08T17:08:28.631772+00:00 heroku[worker.1]: Process exited with status 0
      2018-07-08T17:08:28.723695+00:00 heroku[worker.1]: State changed from up to crashed
      2018-07-08T17:08:28.725787+00:00 heroku[worker.1]: State changed from crashed to starting
      2018-07-08T17:08:31.479079+00:00 heroku[worker.1]: Starting process with command `node bot.js`
      2018-07-08T17:08:32.358515+00:00 heroku[worker.1]: State changed from starting to up
      2018-07-08T17:08:33.922886+00:00 heroku[worker.1]: State changed from up to crashed
      2018-07-08T17:08:33.878013+00:00 heroku[worker.1]: Process exited with status 0
      2018-07-08T17:12:36.000000+00:00 app[api]: Build started by user myemailaddress
      2018-07-08T17:12:48.895075+00:00 app[api]: Release v18 created by user myemailaddress
      2018-07-08T17:12:48.895075+00:00 app[api]: Deploy 1fbb896c by user myemailaddress
      2018-07-08T17:12:49.246800+00:00 heroku[worker.1]: State changed from crashed to starting
      2018-07-08T17:12:49.000000+00:00 app[api]: Build succeeded
      2018-07-08T17:12:53.635812+00:00 heroku[worker.1]: Starting process with command `node bot.js`
      2018-07-08T17:12:54.327615+00:00 heroku[worker.1]: State changed from starting to up
      2018-07-08T17:12:57.874384+00:00 heroku[worker.1]: State changed from up to crashed
      2018-07-08T17:12:57.852555+00:00 heroku[worker.1]: Process exited with status 0
      2018-07-08T17:35:48.475654+00:00 heroku[worker.1]: State changed from crashed to starting
      2018-07-08T17:35:52.257156+00:00 heroku[worker.1]: Starting process with command `node bot.js`
      2018-07-08T17:35:52.995975+00:00 heroku[worker.1]: State changed from starting to up
      2018-07-08T17:35:56.888137+00:00 heroku[worker.1]: State changed from up to crashed
      2018-07-08T17:35:56.871199+00:00 heroku[worker.1]: Process exited with status 0
  </code></pre>
  
  <p><strong>Bot source code:</strong> 
  <a href="https://github.com/meowwwls/words-of-tori-amos/blob/master/bot.js" rel="nofollow noreferrer">https://github.com/meowwwls/words-of-tori-amos/blob/master/bot.js</a></p>
  
  <pre><code>    const twit = require('twit');
      const getVideo = require('./youtube');
      const { getLyrics } = require('./lyricadder/lyrics');
      const { randomNumber } = require('./helpers');
  
      const config = {
        consumer_key: process.env.consumer_key,
        consumer_secret: process.env.consumer_secret,
        access_token: process.env.access_token,
        access_token_secret: process.env.access_token_secret
      };
  
      const Twitter = new twit(config);
  
      const lyrics = getLyrics();
  
      const tweetLyric = () =&gt; {
        const random = randomNumber(lyrics.length);
        const lyric = lyrics[random];
  
        getVideo(lyric.song).then(response =&gt; {
          const songHash = lyric.song.replace(/\s/g, '');
          const tweet = {
            status: `${lyric.lyric}\n${response} #ToriAmos #${songHash}`
          };
  
          Twitter.post('statuses/update', tweet, (err, data, response) =&gt; {
            if (err) {
              console.log(err);
            }
          });
        });
      };
  
      tweetLyric();
  </code></pre>