C#
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:
- token
- tokenSecret
- consumerKey
- consumerSecret
To get the user profile info we need to get the user id. You can fetch this ID from https://tweeterid.com/ by entering the twitter user name (@username):
- userId=Long
- var user=User.GetUserFromId(userId);
Using a properties or mapping json file makes it easier to send messages to only the users mentioned in the json 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 folder.
The information related to the user is stored as an object, containing the userId for the Twitter account we are going to listen to and an array of the phone numbers to which we want to send the notificationsuser: { userId: userId phoneNumbers: [phoneNumbers] }
Before you can use the API for sending SMS, we need to provide the basic configuration variables:
- App Key (App Key (from the Initiate - App Keys section))
- App Secret (App Key (from the Initiate - App Keys section))
- Default Origin (number to be used to send the messages)
- (Optional) Proxy
MessageHandler class works as a container for the functionalities required to send SMS through the API. For this step we have created two methods:
GetAccessToken()
: This method will take the app Key and app Secret and combine them into a Base64 string, which will then be sent to the "/oauth/token" endpoint to generate and return the authentication tokensendMessage()
: This method makes the POST request to the API to send the SMS; it receives the message to be sent, the number which we want to send the message, and optionally we can define from wich number we want to send the message
On this step we finally connect the twitter API to Live Link 365 , using all the data we collected before.
Create an user stream, this will allow us to start listening to the twitter account activity.
The stream allows us to listen to a variety of events, in this case, we listen to the TweetCreatedByMe; this event only triggers when the user tweets or retweets from its account.For each new tweet posted on twitter from the account, a new SMS will be sent to the list of phone numbers defined on the properties.json file
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
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 |
|
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 |
|