Introduction

This tutorial will cover how you can message a contact using FlockOS APIs.

Handling App Installs and Tokens

Before you proceed further, please go through Handling App Installs and Tokens if you haven't already.

To discover and message contacts, we need to call Methods, which we can't do without saving authentication tokens, which is what gets covered in that tutorial.

Discover your contacts' identifier

After installing the app into your own account from the developer dashboard and saving your authentication token, the first thing you need to do is decide which contact to send a message to. Once you've done that you will need that contact's identifier.

We call roster.listContacts to get details about all of our contacts, including their identifiers.

Only the authentication token discovered during app.install is required to call this method.

curl
curl -d token=<token> https://api.flock.co/v1/roster.listContacts
node.js
flock.roster.listContacts('<token>', null, function (error, response) { 
    if (error) {
        console.log('error: ', error);
    } else {
        console.log(response);
    }
});

The response to this method returns an array of PublicProfile objects (an example is shown below). You can retrieve the identifier for the contact from the id field of this object.

{ "id": "u:xxxxxxxxx",
  "firstName": "Bhavin",
  "lastName": "Turakhia",
  "profileImage": "https://f.flockusercontent.com/xxxxxx" }

Send a message to the contact

To send a message, you need to call chat.sendMessage. Using your authentication token and the identifier from the previous step, you can easily do this.

curl
curl -v -d token=<token> -d to=u:knn55lnl35o9559n -d text=hello https://api.flock.co/v1/chat.sendMessage
node.js
flock.chat.sendMessage('<token>', { 
    to: 'u:xxxxxxx', 
    text: 'hello' 
}, function (error, response) { 
    if (error)
        console.log('error: ', error);
    else
        console.log(response);
})

The method returns a globally unique identifier for the message in the uid field. If the message includes an Attachment, the uid can be used to later identify the message when a user clicks on an attachment button, or opens an attachment widget, or takes a FlockML action.