0
votes

I've written a twitter bot in node which retweets some tweets returned from search. I've bundled it with Webpack and deployed it using AWS Lambda. I've set cloudWatch event to trigger the function every hour. I've logged the start and end of the function, and also every time the function successfully/unsuccessfully retweets.

It works when I run it locally. It works when I test it on Lambda management console. However in my CloudWatch Logs, there is no long between the start and end logs. Interestingly, if I decrease the rate of event trigger to <5 minutes, it works again. I'm very confused. Any Idea how I could fix this behaviour?

Here's the logs on the test. The function works as expected.

START RequestId: 8f2a4423-5aca-447e-9d24-a18b8c5ff510 Version: $LATEST
2019-01-23T18:18:42.111Z    8f2a4423-5aca-447e-9d24-a18b8c5ff510    Start
2019-01-23T18:18:42.312Z    8f2a4423-5aca-447e-9d24-a18b8c5ff510    Error retweeting
2019-01-23T18:18:42.312Z    8f2a4423-5aca-447e-9d24-a18b8c5ff510    You have already retweeted this Tweet.
2019-01-23T18:18:42.352Z    8f2a4423-5aca-447e-9d24-a18b8c5ff510    Error retweeting
2019-01-23T18:18:42.352Z    8f2a4423-5aca-447e-9d24-a18b8c5ff510    You have already retweeted this Tweet.
2019-01-23T18:18:42.352Z    8f2a4423-5aca-447e-9d24-a18b8c5ff510    Error retweeting
2019-01-23T18:18:42.352Z    8f2a4423-5aca-447e-9d24-a18b8c5ff510    You have already retweeted this Tweet.
2019-01-23T18:18:42.352Z    8f2a4423-5aca-447e-9d24-a18b8c5ff510    End
END RequestId: 8f2a4423-5aca-447e-9d24-a18b8c5ff510
REPORT RequestId: 8f2a4423-5aca-447e-9d24-a18b8c5ff510  Duration: 881.20 ms Billed Duration: 900 ms     Memory Size: 128 MB Max Memory Used: 64 MB  

Here's my code.

const learnInPublicRetweet = async () => {
  const query = '#SomeHashtag';
  const params = {
    q: query,
    result_type: 'recent',
    lang: 'en'
  };
  console.log('Start');
  let data = await T.get('search/tweets', params);
  const { statuses } = data.data;
  statuses.forEach(async ({ id_str: id, user }) => {
    if (user.id_str !== '1032750379597946880') {
      try {
        await T.post('statuses/retweet/:id', { id });
        console.log(`Retweeted tweet with id ${id}`);
      } catch (err) {
        console.log('Error retweeting');
        console.log(err.message);
      }
    }
  });
  console.log('End');
};

exports.retweet = learnInPublicRetweet;

Here;s my cloudwatch logs


16:44:13 START RequestId: 34df836d-c9b3-4b9a-9547-8f3be7ed5297 Version: $LATEST
16:44:14 2019-01-23T16:44:14.159Z   34df836d-c9b3-4b9a-9547-8f3be7ed5297    Start
16:44:14 2019-01-23T16:44:14.938Z   34df836d-c9b3-4b9a-9547-8f3be7ed5297    End
16:44:14 END RequestId: 34df836d-c9b3-4b9a-9547-8f3be7ed5297

1

1 Answers

1
votes

There is a couple of things probably wrong in your code.

  1. Global variable caching, read more on best practices

    Try change it: async function learnInPublicRetweet() { ... }

  2. You are using forEach loop which I am sure do not works with async call. It will fire off all the async calls and immediately return. You will want to use for .. of or for .. in if iterating over enumerable properties of an object.