/twitterbot_for_slack

setting up a twitter bot to monitor multiple twitter account in real time and get real time notification in slack

Primary LanguagePython

Twitterbot_for_slack

setting up a twitter bot to monitor multiple twitter account in real time and get real time notification in slack

screen shot 2018-10-22 at 2 47 10 pm

IF you are reading this blog then you probably looking for developing some kind of tweet monitor bot for slack. In this article i am going to describe how i created a dynamic tweet monitor bot in python to monitor clients if they are tweeting about something particular. suppose you want to monitor one of your client about when they tweet about something in particular. If you want to monitor just one account then its fine but think about a situation when you are monitoring 100 of twitter accounts. You are definitely going to miss the important tweets in that tweet flood in your slack channel. Suppose you are monitoring Zara and wants to know when they are tweeting about sale. you just want a slack notification when all those big companies like Zara, Columbia, North face etc will be tweeting about sale. That could be any kind of company. So, you don't want to write 50 scripts for 50 companies instead you will create one dynamic script which will be doing the job for all the clients. Now, what if you want to add or remove certain customers from your watch list? we won;'t touch the script but we will be maintaining a CSV file or any kind of database and it will serve the purpose.

1.) First we need few details like below which can be created by going on twitter and follow the instruction on https://chimpgroup.com/knowledgebase/twitter-api-keys/

Consumer:

CONSUMER_KEY = 'consumer_key' CONSUMER_SECRET = 'consumer_secret'

Access:

ACCESS_TOKEN = 'access_token' ACCESS_SECRET = 'access_secret'

2.) To interact with slack we need to create a slack bot API which you can create by following instruction on the below youtube video :- https://www.youtube.com/watch?v=nyyXTIL3Hkw

SLACK API = 'xoxb-gfgdfgdfgdgfdfg' you will get your slack api something like above starting with xoxb.

3.) we will also be maintaining a csv file in which we will be maintaining client twitter name information in the format like below:- screen shot 2018-10-22 at 3 05 50 pm

4.) we need some libraries to set up the whole process. So, please install the below libraries into your environment:-

screen shot 2018-10-22 at 3 09 37 pm

5.) After that we will declare the secret keys of the twitter developer account and slack API. you can use any account but if you want to use Vivox's detail, please contact JIM. you can always create slack API on your own and i have shared the link above for taking reference on how to create a slack API key.

screen shot 2018-10-22 at 3 10 53 pm

6.) Then we will read the CSV file into a panda's dataframe. The CSV file is having all the details of the customer twitter name details and if you need to add or delete any twitter name, you can do that in the CSV file and there will be no need to edit the python script.

screen shot 2018-10-22 at 3 11 46 pm

7.)Then we will define the tweepy function which we will use as extractor later :- screen shot 2018-10-22 at 3 12 14 pm

  1. ) Now, we will create a datafrme "data" :-

screen shot 2018-10-22 at 3 13 50 pm

9.) Now, Twitter provides various details with every tweet. So, If you want to see what all details every tweet is having you can use the below code and see screen shot 2018-10-22 at 3 14 22 pm

10.) Now, whatever information you want to extract and add as a column into the dataframe with the below code. I just have added three details but you can add as much as you want. screen shot 2018-10-22 at 3 15 02 pm

11.) We do not want any tweet which is done by any user or if our client in tweeting in response to any user's tweet. To ignore all that kind of tweets, use below code:-

screen shot 2018-10-22 at 3 15 42 pm

12.) Now, we do not want the same tweet as notification twice so I am restriction my tweet window to check for the last 1 minute tweet only and the cron job is running every minute. The issue here is twitter data is in different time zone and our system time zone is different. So, I am changing both the time in a common format which is UTC.

screen shot 2018-10-22 at 3 16 26 pm

13.) After we are done with this step then we will be creating a list of values on the base of which we will filter the tweets as we wants tweets which is having certain words in it like downtime, maintenance etc.

screen shot 2018-10-22 at 3 19 25 pm

14.) In the last step we will just check if there is any data in the data frame "ndata" and send it as slack notification:- screen shot 2018-10-22 at 3 17 24 pm

15.) Once you are done with all of teh above step, setup the cron job by going to your terminal and use command :-

screen shot 2018-10-22 at 3 49 40 pm

16.) Then make an entry like below

screen shot 2018-10-22 at 3 50 23 pm

END Notes:- ) Make sure that any of the account name is not invalid or private. ) Modify your my_list as per your requirement.

Please let me know if you got any error in the comment section. Please add any valuable suggestion how i can even improve this script. I am graduate student at Northeastern University and currently working as a Data Scientist at Vivox.

Linkedin - https://www.linkedin.com/in/abhimanyu0301/