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.
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.
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