Python
This walkthrough will show you how to build an app that listens for tweets from a certain Twitter account, and then routes that tweets to a phone number through Live Link 365's SMS API.
This is not a production-ready application. Please take your time to enhance it for production so that it meets your specific business requirements.
Steps
Before you can access the Twitter Streaming API, you will need to get the following information from Twitter:
- API key
- API secret
- Access token
- Access token secret
- Go to the official Twitter Apps website and log in with your Twitter credentials. If you do not already have a Twitter account, you will need to create one
- Click on Create New App
- Complete the Create an Application form, agree to the terms and conditions, and click on Create your Twitter Application
-
Once the new application is created, a new page for the app is displayed. Click on the Keys and Access Tokens tab and make a careful note of your
API key
andAPI secret
- Scroll down to the Token Actions section and click Create my access token. Carefully note down your "Access token" and "Access token secret".
Enter your credentials in the code into:
- access_token
- access_token_secret
- consumer_key
- consumer_secret
The streaming API requires Twitter IDs rather than user names to work. You can fetch these IDs from https://tweeterid.com/
by entering the Twitter user name [@user]:
Using a properties or mapping file makes it easier to send messages to only the users contained in the mapping file. This is done manually because it is very difficult to do it directly via the API since not all the users keep their mobile numbers public.
Properties file can be kept at the same place where the Python scripts are.
- @john_doe=[phone_number]
Before you can use the API for sending SMS, you have to fetch an authentication token, by providing:
- App Key (from the Authentication > App Key and Secret section)
- App Secret (from the Authentication > App Key and Secret section)
getAccessToken()
method will take the app key and app secret and combine them into a Base64 string, which will then be sent to the /api/oauth/token
to generate the authentication token
For sending the SMS we will make a method that will take 3 attributes:
- number: the number the SMS will be sent to
- message: the message that the destination phone number should receive
- token: The authentication token that will be generated with the method explained in the previous step
/api/v2/sms
endpoint, and we will call it for each new message we need to send
On this step we finally connect the Twitter API to Live Link 365 , using all the methods we built before.
The on_status()
method will keep listening for the tweets.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|