This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Tweet Based on Retrieved Twitter Data

This example shows how to retrieve the number of followers for a Twitter® account and Tweet® about achieving a specific follower count. You can adapt this example to retrieve data from other Twitter REST API endpoints, such as collections, lists, and so on.

To run this example, you need Twitter credentials. To obtain these credentials, you must first log in to your Twitter account. Then, fill out the form in Create an application.

Connect to Twitter

Create a Twitter connection using your credentials. (The values in this example do not represent real Twitter credentials.)

consumerkey = 'abcdefghijklmnop123456789';
consumersecret = 'qrstuvwxyz123456789';
accesstoken = '123456789abcdefghijklmnop';
accesstokensecret = '123456789qrstuvwxyz';

c = twitter(consumerkey,consumersecret,accesstoken,accesstokensecret);

Check the Twitter connection. If the StatusCode property has the value OK, the connection is successful.

c.StatusCode
ans = 

    OK

Retrieve Number of Followers

Set the Twitter base URL to access the GET followers/ids REST API endpoint. Search for a specific Twitter account using the Twitter connection object, base URL, and screen name. (The screen name in this example does not represent real Twitter data.)

baseurl = 'https://api.twitter.com/1.1/followers/ids.json';
sname = 'screenname';
d = getdata(c,baseurl,'screen_name',sname)
d = 

  ResponseMessage with properties:

    StatusLine: 'HTTP/1.1 200 OK'
    StatusCode: OK
        Header: [1×25 matlab.net.http.HeaderField]
          Body: [1×1 matlab.net.http.MessageBody]
     Completed: 0

d is a matlab.net.http.ResponseMessage object. The StatusCode property shows OK, indicating a successful request.

Determine the number of followers for the specified account.

numfollowers = length(d.Body.Data.ids)
numfollowers =

    44

This account has 44 followers.

Post Tweet

Create the character vector tweetString, which specifies the Tweet to post. If the number of followers is greater than 25, then the Tweet indicates the screen name has more than 25 followers. Otherwise, it indicates that the screen name needs more followers.

if numfollowers > 25
    tweetString = [sname ' has more than 25 followers!'];
else
    tweetString = [sname ' needs more followers!'];
end

Set the Twitter base URL to access the POST statuses/update REST API endpoint.

baseurl = 'https://api.twitter.com/1.1/statuses/update.json';

Tweet about the number of followers using the Twitter connection object, base URL, and tweetString.

d = postdata(c,baseurl,'status',tweetString)
d = 

  ResponseMessage with properties:

    StatusLine: 'HTTP/1.1 200 OK'
    StatusCode: OK
        Header: [1×22 matlab.net.http.HeaderField]
          Body: [1×1 matlab.net.http.MessageBody]
     Completed: 0

d is a matlab.net.http.ResponseMessage object. The StatusCode property shows OK, indicating a successful request.

See Also

Functions

Objects

Related Topics

External Websites