I will show you how to implement a Twitter bot that runs on Heroku in Python. I think it's exhausted, but it's the 2016 version.
The solution is to deploy a Python script on Heroku and use Heroku Scheduler to run the script on a regular basis.
The source code of the completed version is below. https://github.com/k-enomoto/minimum_twitter_bot
--Minimum source code for Twitter Bot --Deploy to Heroku --Heroku Scheduler settings
--How to use Heroku --How to use pip --How to create a Twitter API account
There are other detailed articles for each, so please refer to them.
Install the library you want to use.
$ pip install python-twitter
$ pip install bottle
We need to tell Heroku which library to use, so create requirements.txt
.
$ pip freeze -l > requirements.txt
Maybe it will be like this.
bottle==0.12.9
future==0.15.2
oauthlib==1.1.2
python-twitter==3.1
requests==2.10.0
requests-oauthlib==0.6.2
Get the Twitter API consumer_key
, consumer_secret
, ʻaccess_token_key, ʻaccess_token_secret
in advance.
Please refer to the following article for the acquisition method.
Tweet using Twitter API in Ruby
Also, each key is set in an environment variable from the viewpoint of security. How to set environment variables on Heroku will be described later.
Please feel free to arrange the content of the tweet as soon as you take it from the Web API.
tweet.py
# -*- coding: utf-8 -*-
import os
from datetime import datetime
import twitter
api = twitter.Api(consumer_key=os.environ["CONSUMER_KEY"],
consumer_secret=os.environ["CONSUMER_SECRET"],
access_token_key=os.environ["ACCESS_TOKEN_KEY"],
access_token_secret=os.environ["ACCESS_TOKEN_SECRET"]
)
api.PostUpdate("system time is %s" % datetime.now())
This is the point of this time. Prepare a dummy web app to keep the process running on Heroku. In this article, we used the lightweight web framework bottle. The sample code in bottle documentation is used as is.
index.py
# -*- coding: utf-8 -*-
import os
from bottle import route, run
@route("/")
def hello_world():
return "" #Whatever you return here
run(host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))
Procfile The Procfile contains the commands to be executed in the Heroku container. The command to start the above dummy web application is described.
Procfile
web: python index.py
runtime.txt
This is point 2.
Create runtime.txt
and describe the runtime to use.
runtime.txt
python-3.5.2
Now you have all the files you need.
index.py
tweet.py
requirements.txt
runtime.txt
Procfile
Files must be managed in Git in order to deploy to Heroku. Commit to Git.
git add .
git commit -m "Initial commit"
Deploy to Heroku.
Also, the environment variables mentioned above are set here. Replace your_cosumer_key
, your_consumer_secret
, your_access_token_key
, and your_access_token_secret
with your own values.
$ heroku create --stack cedar
$ git push heroku master
$ heroku config:set CONSUMER_KEY=your_cosumer_key CONSUMER_SECRET=your_consumer_secret ACCESS_TOKEN_KEY=your_access_token_key ACCESS_TOKEN_SECRET=your_access_token_secret
Make sure that you can deploy properly.
$ heroku logs
Finally, register the task with Heroku Scheduler. Enter the following command to bring up the scheduler registration page.
$ heroku addons:create scheduler:standard
$ heroku addons:open scheduler
Set the command to be executed by the scheduler and the frequency. Let's set up a Python script to tweet.
$ python tweet.py
This completes the Twitter bot.
Recommended Posts