Use Heroku in python to notify Slack when a specific word is muttered on Twitter

** This article was posted on November 23, 2019. ** **

Hello. This is @kosukeobata. Recently, there are many titles such as PdM and marketer, but since engineering has resumed, I will post a little ridiculous.

This time, I will describe how to make a BOT that notifies Slack when a specific word is muttered on Twitter. It's not very beautiful on GitHub, but since the source code is open to the public, I think you can make it while referring to it. https://github.com/kosukeobata/twitter-bot

■ Requirements

--Slack notification when a particular word is posted on Twitter. --Tweets with duplicate tweet ids should not be notified. --It doesn't have to be a real-time notification.

■ Technology

■ What you are doing

Use Heroku's Scheduled add-on to search Twitter once every 10 minutes. Any posts that get caught will notify the specific Slack you specify.

At that time, save the largest tweet ID among the acquired tweets in sqlite3. By searching only tweets that are larger than the ID saved at the next execution, the sharing of duplicate tweet IDs will be eliminated.

■ Installation procedure

  1. Create a Twitter API account.
  2. Create a Heroku account and add an instance.
  3. Webhook settings in Slack.
  4. Git clone from my repository.
  5. Add config.py file.
  6. Deploy to Heroku.
  7. Add Scheduler to Heroku.

1. Create a Twitter API account

Register from https://developer.twitter.com/ and apply for use. Please note that it takes time from application to approval. Please refer to this Qiita for details. Summary of procedures from Twitter API registration (account application method) to approval * Information as of August 2019

2. Create a Heroku account

Register from https://jp.heroku.com/. There is nothing particularly difficult, so I think you can do it smoothly.

Once you have created an account, let's create an instance. In this article, for example, the instance name will be "qiita-katsuo". So, regarding the part of qiita-katsuo that appears in this article, I think that there is a name you gave yourself, so please replace it with that name and read on.

3. Slack Webhook settings

There are various Qiita articles here as well, so please refer to them. Use Slack's Incoming Webhooks Once you get the URL that starts with "https://hooks.slack.com/services/", you're done.

  1. git clone Go to the directory you want to work with and git clone it.

$ git clone [email protected]:kosukeobata/twitter-bot.git



### 5. Add config.py file
 Since it is a file that describes the access token etc., it is not uploaded on GitHub.
 Please add the file yourself.
 It is ok if the file is as follows.

 ――The top four are Twitter API settings.
 --WEB_HOOK_KEY is Slack's Webhook setting.
 --For SEARCH_KEYWORD, specify the keyword you want to post when you are muttered on Twitter.
 --For CHANNEL_NAME, specify the channel name you want to be notified by Slack.

CONSUMER_KEY = "" CONSUMER_SECRET = "" ACCESS_TOKEN = "" ACCESS_TOKEN_SECRET = "" WEB_HOOK_KEY = "" SEARCH_KEYWORD = "" CHANNEL_NAME = "#*********"


 At this point you can run it locally.
 If you want to test in local, first execute the following command to prepare the environment in local.
 ** Install pip **

$ curl -O https://bootstrap.pypa.io/get-pip.py $ python get-pip.py --user $ export PATH="HOME/Library/Python/2.7/bin:PATH" $ echo 'export PATH="HOME/Library/Python/2.7/bin:PATH"' >> ~/.bash_profile $ source ~/.bash_profile


 ** Install request **
```$ pip install requests requests_oauthlib --user```

 ** Run **

#### **`$ python get_twitter_data.py`**
```py


### 6. Deploy to Heroku
 Deploy to the instance created in step 2.
 First, login authentication is performed in the terminal, then the instance is linked, and then pushed to heroku.

$ heroku login $ git init $ heroku git:remote -a qiita-katsuo $ git add . $ git commit -am "make it better" $ git push heroku master


### 7. Register Schedule on Heroku.
 With the Schedular add-on, you can search Twitter once every 10 minutes.
 It's not real-time, but I think it's okay if it's that frequent.
 First, add the add-on by hitting the following command in the terminal.

#### **`$ heroku addons:create scheduler:standard --app qiita-katsuo`**

Then, you can register the schedule from the heroku screen. https://dashboard.heroku.com/apps/qiita-katsuo/scheduler Specify the following command as the Job to be specified.

$ python get_twitter_data.py



 This completes the settings.
 If you want to change other small settings, please tune it little by little.


Recommended Posts

Use Heroku in python to notify Slack when a specific word is muttered on Twitter
Write a script in Shell and Python to notify you in Slack when the process is finished
Let's use AWS Lambda to create a mechanism to notify slack when the value monitored by CloudWatch is exceeded on Python
Easy! Implement a Twitter bot that runs on Heroku in Python
How to use is and == in Python
[Stada] A script that will call you when Pokemon GO is released
Use Heroku in python to notify Slack when a specific word is muttered on Twitter
A solution when you can't start project Django on Windows
When you want to hit a UNIX command on Python
Write a script in Shell and Python to notify you in Slack when the process is finished
A memorandum because I stumbled on trying to use MeCab in Python
It is convenient to use Layers when putting a library on Lambda
How to notify a Discord channel in Python
Output timing is incorrect when standard (error) output is converted to a file in Python
A memorandum on how to use keras.preprocessing.image in Keras
Things to note when initializing a list in Python
Use communicate () when receiving output in a Python subprocess
A script that transfers tweets containing specific Twitter keywords to Slack in real time
Create a list in Python with all followers on twitter
How to use the __call__ method in a Python class
[python] A note when trying to use numpy with Cython
Summary of points to keep in mind when writing a program that runs on Python 2.5
I made a program to collect images in tweets that I liked on twitter with Python
When you want to hit a UNIX command on Python
Post to Slack in Python
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
A note on what you did to use Flycheck with Python
A game to go on an adventure in python interactive mode
Post a message from IBM Cloud Functions to Slack in Python
[Python] Execution time when a function is entered in a dictionary value
How to use python put in pyenv on macOS with PyCall
Timezone specification when converting a string to datetime type in python
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] How to use the in operator in a for statement?
How to deploy a Django app on heroku in just 5 minutes
What to do when the value type is ambiguous in Python?
Use fabric as is in python (fabric3)
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
When writing a program in Python
Use AWS lambda to scrape the news and notify LINE of updates on a regular basis [python]
Things to watch out for when creating a Python environment on a Mac
What to do if there is a decimal in python json .dumps
Connect to postgreSQL from Python and use stored procedures in a loop.
How to write a string when there are multiple lines in python
Notify using Notification Center when the execution environment is macOS in Python
[Python] [Word] [python-docx] Try to create a template of a word sentence in Python using python-docx
Add words to MeCab's user dictionary on Ubuntu for use in Python
How to automatically notify by phone when the python system is down
What is the fastest way to create a reverse dictionary in python?
Notify Slack when the machine learning process running on GCP is finished
What to do when a warning message is displayed in pip list
How to stop a program in python until a specific date and time
How to use the Slack API using Python to delete messages that have passed a certain period of time for a specific user on a specific channel
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
[Introduction to Python] How to use class in Python?
Use print in a Python2 lambda expression
[Python3] Code that can be used when you want to cut out an image in a specific size
The story of creating Botonyan that returns the contents of Google Docs in response to a specific keyword on Slack
Precautions when pickling a function in python
Send a message from Python to Slack
Log in to Slack using requests in Python
How to get a stacktrace in python