Amazon Web Services, AWS, Cloud, Lambda, Python, Script, serverless

Tweet-bot : Re-tweet the tweets contaning keyword using AWS lambda

September 28, 2019

Greetings!

From last week I got the idea to automatically retweet the tweet with particular keyword. In this article we will discuss how to do that using AWS Lambda using Python 3.6 runtime and serverless framework. This is first I have used the serverless framework. Let’s get started.

First the serverless environment should be in place. To install serverless architecture you can visit this link.

Next step is to setup AWS credentials and you can more information here.

Now you should create a twitter account if you don’t have or if you want new account for this development and then go to developer.twitter.com to apply for a developer account to access the Twitter API.

Now you have serverless framework configured, you can go ahead and create a new serverless service on your local machine.


$ serverless create --template aws-python3 --name serverlessbot --path serverlessbot

A new directory will be created. Now cd into new directory and next you create python virtual environment and also activate it.

$ cd serverlessbot
$ virtualenv venv --python=python3
$ source venv/bin/activate

Now install the twython library. You can find more information on this library here. If you just want to read the feed from twitter and use it in your app you can do that also. There is two type of authentication in twython one in which you need read only access and other which we are using to take some action to tweet, retweet etc. Also save the package versions of the environment to a requirements.txt file.$ serverless remove

$ pip install twython
$ pip freeze > requirements.txt

Now we will start coding the re-tweet bot. Configuring the external pyhton libraries is tricky. There are multiple ways to do it. One is you can use the AWS Cloud9 service and directly push the code to lambda or you can create package on your local machine and manually upload the code to lambda function. Here we are using the serverless-python-requirements plugin which is easy way to do it.

The first step is to initiate the npm(accept all the default values) and then install the serverless-python-requirements.

$ npm init $ npm install --save serverless-python-requirements 

Add the following code at the end of serverless.yml file. You can find more information on this plugin here.

plugins:   - serverless-python-requirements custom:   pythonRequirements:     dockerizePip: non-linux 

Another code we need to replace in serverless.yml is of lambda function which is as below

functions:   retweet:     handler: handler.retweet     events:       - schedule: rate(8 hours)  

This code will retweet a tweet with #Quote keyword in it every 8 hours.

ALL the code you can find in my github repository here. You can use the code as it is to deploy in your environment. You need to update the config.py.

To deploy the code to AWS environment you can use the below command.

$ serverless deploy -v 

The Serverless Framework will now take care of everything needed to deploy your Lambda function. Now you can see the lambda function in your environment. There is cloudwatch event configured to trigger the function every 8 hours.

You can manually invoke the function using below command

$ serverless invoke -f retweet 

If you want to remove the function from your AWS account you can simply use:

$ serverless remove 

I hope by now you have a twitter bot up and running.

Happy coding!

You Might Also Like

No Comments

Leave a Reply