Java
This walkthrough will show you how to build an app that listens for tweets from a certain Twitter account, and then routes that tweet 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 https://apps.twitter.com/ and log in with your Twitter credentials.
Note: 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 Create your Twitter application
- Once the new application is created, a new page for the app is displayed. Click the Keys and Access Tokens tab, and make a careful note of your "API key" and "API 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]:
- follow_users_ids = [759251]
- stream.filter(follow=follow_users_ids)
Using a properties or mapping file makes it easier to send messages to only the users mentioned in the mapping file. This is done manually because it’s very difficult to do it directly via the API since not all the users keep their mobile numbers public.
Properties file is kept in the resources package:
- @john_doe=phone_number
Before you can use the API for sending SMS, you have to fetch an authentication token, by providing:
- App Key (Authentication > App Key and Secret)
- App Secret (Authentication > App Key and Secret)
getAccessToken()
method will take the app key and secret and combine them into a Base64 string, which will then be sent to the /api/oauth/token
for generating 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 POST to 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 onStatus()
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|