** 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
--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.
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.
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
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.
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.
$ 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="
** 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