I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7

Recently I started playing with Python, so I wanted to make a Twiiter Bot as a starting point.

However, it's too lazy to manage a server just to run a bot, so I made one that can be made with AWS Lambda.

$ tree .
.
├── README.md
├── deploy.py
├── event.json
├── lambda.json
├── lib
│   └── __init__.py
├── mezamashidokei_character.png
├── requirements.txt
├── setup.py
└── tweet.py

With it, Hello World will create a Twitter Bot that will tweet the current time every 30 minutes.

It feels like a so-called boilder plate because it allows you to do everything from uploading to scheduling settings with a single command.

If you play with this for the time being, you should be able to learn the color of bot development with Lambda + Python.

How to use

For the time being, the most troublesome thing is the first setup.

--Create a virtual environment with virtualenv -Look around here --Create IAM (the one with AWS LambdaBasicExecutionRole) -[Look around here](http://qiita.com/yutackall/items/a4039b917cb8848c3c4d#lambda-%E5%AE%9F%E8%A1%8C%E7%94%A8%E3%83%AD%E3% 83% BC% E3% 83% AB% E4% BD% 9C% E6% 88% 90) --Create Twitter App -Look around here --aws cli settings -[Look around here](http://qiita.com/n0bisuke/items/1ea245318283fa118f4a#cli%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83 % 88% E3% 83% BC% E3% 83% AB)

Unfortunately, these have to be done manually. However, I think that there are many people who have already set up AWS and VirtualEnv, so it's actually just the Twitter App.

Well, let's ask him to do it while looking at various links, and set the ID and access token obtained there in the .env file.

--Example of .env

# AWS settings
AWS_ACCOUNT_ID=12345678910
AWS_IAM_ROLE_NAME=lambda_basic_execution
AWS_REGION_NAME=ap-northeast-1

# Twitter account name
TWITTER_ACCOUNT_NAME=yamadatarou01234

# Twitter app consumer key, consumer secret
CONSUMER_KEY=hogehoge01234
CONSUMER_SECRET=fugafuga01234

# Twitter account tokens
ACCESS_TOKEN=nyannyan01234
ACCESS_TOKEN_SECRET=wanwan01234

# [Options] Virtualenv path
#It can be empty here
CUSTOM_VENV_PATH=

After that, install the dependency.

$ pip install -q -t ./lib -r ./requirements.txt

At this point, type the setup command and

$ python setup.py

I've deployed it.

$ python deploy.py
Upload?(y/n):y
Shedule?(y/n):y

It will be fine and an account like this will be created.

kako-cWQSOOdiY3DhN8yM.png

After this

After the first deployment, all you have to do is modify it and develop it.

When developing locally, you can execute the same environment as lambda locally by typing the following command.

$ python-lambda-local -f lambda_handler  ./tweet.py ./event.json

Also, if you want to deploy again, you can easily upload it by typing the deploy command.

$ python deploy.py
Account Name:your_twitter_bot_name
Upload?(y/n):y
Shedule?(y/n):n

Since the library uses Tweepy, it is good to concentrate on creating bots while reading API Reference.

As for the function to perform Tweet / Retweet / Favorite / Refollow, I added a function to tweet.py for your reference.

It's not good that you can't concentrate on what you really want to do with deployment and various settings, so writing and developing such a simple script will make progress.

Recommended Posts

I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
Steps to create a Twitter bot with python
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
I tried to build a Mac Python development environment with pythonz + direnv
I wrote a Slack bot that notifies delay information with AWS Lambda
I made a Twitter BOT with GAE (python) (with a reference)
I want to create a nice Python development environment for my new Mac
I made a bot to post on twitter by web scraping a dynamic site with AWS Lambda (continued)
When I tried to create a virtual environment with Python, it didn't work
I want to AWS Lambda with Python on Mac!
Quickly create a Python data analysis dashboard with Streamlit and deploy it to AWS
Script to easily create a client device environment for AWS IoT (Python v2 version)
[AWS] Development environment version that tried to build a Python environment with eb [Elastic Beanstalk]
Create a Mastodon bot with a function to automatically reply with Python
Create a Twitter BOT with the GoogleAppEngine SDK for Python
How to build a python2.7 series development environment with Vagrant
Create a simple Python development environment with VSCode & Docker Desktop
Create API with Python, lambda, API Gateway quickly using AWS SAM
Create a virtual environment with Python!
I wrote a script to get you started with AtCoder fast!
Try to create a python environment with Visual Studio Code & WSL
I made a Twitter Bot with Go x Qiita API x Lambda
I wrote a function to load a Git extension script in Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I wrote a script to extract a web page link in Python
Create a simple Python development environment with VS Code and Docker
[Python] I wrote a REST API using AWS API Gateway and Lambda.
[For Python] Quickly create an upload file to AWS Lambda Layer
How to create a serverless machine learning API with AWS Lambda
[Python] Create a virtual environment with Anaconda
I want to build a Python environment
I want to play with aws with python
Connect to s3 with AWS Lambda Python
Let's make a Twitter Bot with Python!
I tried to delete bad tweets regularly with AWS Lambda + Twitter API
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I was addicted to creating a Python venv environment with VS Code
I tried to create a plug-in with HULFT IoT Edge Streaming [Development] (2/3)
Steps to create a Python virtual environment with VS Code on Windows
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
[To Twitter gentlemen] I wrote a script to convert .jpg-large to .jpg at once.
[Outlook] I tried to automatically create a daily report email with Python
Macbook Air with M1 is here! Quickly create a Python computing environment
How to create a Python virtual environment (venv)
Make a Twitter trend bot with heroku + Python
Create a LINE BOT with Minette for Python
I want to create a window in Python
I made a Twitter fujoshi blocker with Python ①
I want to make a game with Python
Create a virtual environment with conda in Python
[Python] Build a Django development environment with Docker
Create a python3 build environment with Sublime Text3
LINE BOT with Python + AWS Lambda + API Gateway
I wrote a script to upload a WordPress plugin
Quickly build a Python Django environment with IntelliJ
I want to write to a file with Python
Create a Python execution environment on IBM i
Create a Python virtual development environment on Windows
[Note] How to create a Mac development environment
I made a Mattermost bot with Python (+ Flask)
Get a quick Python development environment with Poetry