HTTP Trigger

HTTP Triggers are a way to remotely invoke a workflow using a POST command.

You can call this from a web application, or using a utility like curl:

$ curl -X POST \
-H "authorization: Bearer $token" \
"https://$relay/workflow/wf_message_qQruCT57FebWVfyRuloP1B?subscriber_id=7b28d9b0-4b46-41f8-910c-bcf8dacXXXXX&user_id=99000756002XXXX" \
-H "Content-Type: application/json" \
-d "{
      'action': 'invoke',
      'target_device_ids': [\"99000756002XXXX\"]
    }"

Let's walk through the creation and To create an HTTP trigger, first you'll want to create a workflow and host it on your webserver. Here we have a simple workflow that just speaks out 'Hello ':

export default createWorkflow(relay => {
  const workflowName = `message`
  relay.on(Event.START, async () => {
    deviceName = await relay.getDeviceName()
    relay.say(`Hello ${deviceName}`)
  })
})

Next, you will register your workflow against an HTTP trigger using the Relay CLI:

$ relay workflow:create:http -n message -u wss://myappserver.com/message --trigger=POST -i 99000756002XXXX

Your CLI command will return a workflow ID - which will be part of the URI that you'll call to trigger your workflow:

=== Installed Workflow
ID                                Name    Type      Uri                           Args Installed on
wf_message_qQruCT57FebWVfyRuloP1B message http:POST wss://myappserver.com/message       99000756002XXXX

Next we'll add a call to our newly created HTTP trigger

import querystring from 'querystring'
import axios from 'axios'

const ibot = 'https://all-api-qa-ibot.nocell.io'
const workflow = '/ibot/workflow/wf_message_qQruCT57FebWVfyRuloP1B' 
const auth_endpoint = 'https://auth.republicdev.info/oauth2/token'
const sub_id = //Your Subscriber ID
const device_id = //Your Device ID
let refresh_token

const params = querystring.stringify({
  'subscriber_id': sub_id,
  'user_id': device_id
})

async function send_relay_message() {
  let access_token = await get_access_token()
  try { 
    const response = await axios.post(`${ibot}${workflow}?${params}`,
    {
      "action": "invoke",
      "target_device_ids": ["${device_id}"]
    },
    { 
      headers : {
        'Authorization': 'Bearer ' + access_token
      }
    })
    if (response.status == 200 || response.status == 400) {
      console.log(`HTTP trigger invoked!`)
      
    }
  } catch (e) {
    console.error(e)
  }
  res.send('Recieved')
}

async function get_access_token() {
   try {
    if (!refresh_token){
      const response = await axios({
        method: 'post',
        headers: {
            'content-type' : 'application/x-www-form-urlencoded', 
            'Authorization': `Basic ${process.env.TOKEN}`
        },
        url: auth_endpoint,
        data: qs.stringify({
            grant_type: 'password',
            client_id: process.env.CLIENT_ID,
            scope: 'openid',
            username: process.env.TOKEN_USERNAME, //Your account username
            password: process.env.TOKEN_PASS //Your account password
        }),
    })
      refresh_token = response.data.refresh_token
      return response.data.access_token
    } else { 
      //TODO Add refresh token grant
    }
  } catch (e) {
    console.error(e)
  }
}