SXAPI Resource : Twitter API

This resource allow you to interact with the Twitter API Webservice. Programmers can access resource methods and embed this module methods into there own method and endpoints. API developpers can use resource endpoints into there configuration profile to expose Twitter API data.

This resource is based on twitter npm module npm and is part of the sxapi-core engine from sxapi.

Resource configuration

To configure this resource, you must add a config key under the resources section of your configuration profile. This key must be a unique string and will be considered as the resource id. The value must be an object who must have the appropriate configuration parameters.

For a better understanting of the sxapi configuration profile, please refer to the configuration guide

This config object will be passed to require('twitter').Twitter() method of the nodejs twitter module. Read twitter Twitter documentation for a complete list of the parameters that you can use in this config object.

Resource config parameters

Param Mandatory Type default Description
_class yes string module name. Must be twitter for this resource
consumer_key yes string your Twitter consumer key.
consumer_secret yes string your Twitter consumer secret.
access_token_key no string your Twitter token key.
access_token_secret no string us-west-1 your Twitter token secret.
... no N/A any Twitter option. See see twitter Twitter documentation.

Example

This is a sample configuration of this resource. You must add this section under the resources section of your configuration profile

resources:
  twitter-id:
    _class: twitter
    consumer_key: xxxxxxxxxxx
    consumer_secret: yyyyyyyyyyyy
    access_token_key: xxxxxxxxxxx
    access_token_secret: yyyyyyyyyyyy

Resource methods

If you want to use this resource in our own module, you can retrieve this resource instance by using $app.resources.get('twitter-id') where twitter-id is the id of your resource as defined in the resource configuration.

This module come with several methods for manipulating aws Twitter resources.

0. readStream method
1. read method
2. removeMessage method
3. sendMessage method
4. listQueues method
5. createQueue method
6. deleteQueue method

Method readStream

get a stream of tweet for a matching expression.

Parameters

Param Mandatory Type default Description
match yes object the matching expression looking for
callback no function default callback function called when server answer the request.
If not defined, dropped to a default function who output information to the debug console
callback(error,response) N/A mixed null will be false or null if no error returned from the Twitter API Webservice. Will be a string message describing a problem if an error occur.
callback(error,response) N/A mixed a list of messages from the queue

Example

var resource = $app.resources.get('twitter-id');
resource.readStream(
    {match:"#paris"}, 
    function (error, response) {
        console.log(error, response);
    });

Method read

get a list of message for a list queue.

Parameters

Param Mandatory Type default Description
options no object Configuration option passed to the Twitter API.receiveMessage method. Read documentation for more options
options.QueueUrl no string the queue url to read message from. If not defined will use the default resource queueUrl
callback no function default callback function called when server answer the request.
If not defined, dropped to a default function who output information to the debug console
callback(error,response) N/A mixed null will be false or null if no error returned from the Twitter API Webservice. Will be a string message describing a problem if an error occur.
callback(error,response) N/A mixed a list of messages from the queue

Example

var resource = $app.resources.get('twitter-id');
resource.read(
    {QueueUrl:"https://sqs.eu-west-3.amazonaws.com"}, 
    function (error, response) {
        console.log(error, response);
    });

Method removeMessage

Remove a message from a list queue.

Parameters

Param Mandatory Type default Description
options yes object Configuration option passed to the Twitter API.deleteMessage method. Read documentation for more options
options.ReceiptHandle yes string the message Id to remove
options.QueueUrl no string the queue url to delete message from. If not defined will use the default resource queueUrl
callback no function default callback function called when server answer the request.
If not defined, dropped to a default function who output information to the debug console
callback(error,response) N/A mixed null will be false or null if no error returned from the Twitter API Webservice. Will be a string message describing a problem if an error occur.
callback(error,response) N/A mixed meta informations about the result of this deletion

Example

var resource = $app.resources.get('twitter-id');
resource.removeMessage(
    {ReceiptHandle:"df654s8#9d23s43f3mgh?66se63"}, 
    function (error, response) {
        console.log(error, response);
    });

Method sendMessage

Send a message to the given queue.

Parameters

Param Mandatory Type default Description
message yes object The message object
options yes object Configuration option passed to the Twitter API.sendMessage method. Read documentation for more options
options.QueueUrl no string the queue url where message should be inserted. If not defined will use the default resource queueUrl
callback no function default callback function called when server answer the request.
If not defined, dropped to a default function who output information to the debug console
callback(error,response) N/A mixed null will be false or null if no error returned from the Twitter API Webservice. Will be a string message describing a problem if an error occur.
callback(error,response) N/A mixed meta informations about the result of this insertion

Example

var resource = $app.resources.get('twitter-id');
resource.sendMessage(
    { id : "msg1", "key" : "value" }, 
    { QueueUrl:"https://sqs.eu-west-3.amazonaws.com" }, 
    function (error, response) {
        console.log(error, response);
    });

Method listQueues

get a list of all queues availables.

Parameters

Param Mandatory Type default Description
options no object Configuration option passed to the Twitter API.listQueues method. Read documentation for more options
callback no function default callback function called when server answer the request.
If not defined, dropped to a default function who output information to the debug console
callback(error,response) N/A mixed null will be false or null if no error returned from the Twitter API Webservice. Will be a string message describing a problem if an error occur.
callback(error,response) N/A mixed a list of messages from the queue

Example

var resource = $app.resources.get('twitter-id');
resource.listQueues({}, function (error, response) {
        console.log(error, response);
    });

Method createQueue

Create a new message queue

Parameters

Param Mandatory Type default Description
options yes object Configuration option passed to the Twitter API.createQueue method. Read documentation for more options
callback no function default callback function called when server answer the request.
If not defined, dropped to a default function who output information to the debug console
callback(error,response) N/A mixed null will be false or null if no error returned from the Twitter API Webservice. Will be a string message describing a problem if an error occur.
callback(error,response) N/A mixed meta informations about the result of this insertion

Example

var resource = $app.resources.get('twitter-id');
resource.createQueue({ }, function (error, response) {
        console.log(error, response);
    });

Method deleteQueue

Delete a message queue.

Parameters

Param Mandatory Type default Description
options yes object Configuration option passed to the Twitter API.deleteQueue method. Read documentation for more options
callback no function default callback function called when server answer the request.
If not defined, dropped to a default function who output information to the debug console
callback(error,response) N/A mixed null will be false or null if no error returned from the Twitter API Webservice. Will be a string message describing a problem if an error occur.
callback(error,response) N/A mixed meta informations about the result of this insertion

Example

var resource = $app.resources.get('twitter-id');
resource.deleteQueue({ }, function (error, response) {
        console.log(error, response);
    });

Resource endpoints

This module come with 4 endpoints who can interact with any twitter method.

1. listMessages endpoint
2. addMessage endpoint
3. deleteMessage endpoint
4. listQueue endpoint
5. addQueue endpoint
6. deleteQueue endpoint

listMessages endpoint

The purpose of this endpoint is to make call to a Twitter API Webservice and to return the a list of message from a given queue.

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "get"
config no string Configuration object to pass to the read resource method (see options)

Example

server:
  endpoints:
  - path: "/twitter"
    resource: twitter-id
    endpoint: listMessages
    config: {}

addMessage endpoint

The purpose of this endpoint is to insert a key into a Twitter API Webservice. Document will be the HTTP body of the query.

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "create"
config no string Configuration object to pass to the sendMessage resource method (see options)

Example

server:
  endpoints:
  - path: "/twitter/:id"
    method: POST
    resource: twitter-id
    endpoint: addMessage

deleteMessage endpoint

The purpose of this endpoint is to delete a message from Twitter API queue. Id is defined by the context.

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "delete"
config no object Configuration option passed to the Twitter API.deleteMessage method. Read documentation for more options

Example

server:
  endpoints:
  - path: "/twitter/:id"
    method: DELETE
    resource: twitter-id
    endpoint: deleteMessage

listQueue endpoint

The purpose of this endpoint is to make call to a Twitter API Webservice and to return the a list of availables queues.

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "get"
config no string Configuration object to pass to the listQueues resource method (see options)

Example

server:
  endpoints:
  - path: "/twitter"
    resource: twitter-id
    endpoint: listQueue
    config: {}

addQueue endpoint

The purpose of this endpoint is to insert a key into a Twitter API Webservice. Id is defined by the context.

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "create"
config no string Configuration object to pass to the createQueue resource method (see options)

Example

server:
  endpoints:
  - path: "/twitter/:id"
    method: POST
    resource: twitter-id
    endpoint: addQueue

deleteQueue endpoint

The purpose of this endpoint is to delete a complete Twitter API queue. Id is defined by the context.

Parameters

Param Mandatory Type default Description
path yes string path used as client endpoint (must start with /)
resource yes string resource id declared in the resource of your config profile
endpoint yes string endpoint name declared in the resource module. In this case must be "delete"
config no object Configuration option passed to the Twitter API.deleteQueue method. Read documentation for more options

Example

server:
  endpoints:
  - path: "/twitter/:id"
    method: DELETE
    resource: twitter-id
    endpoint: deleteQueue