Skip to content

Example of how to connect an incoming SMS in Flex to a cellular IoT device using an Azure Function.

Notifications You must be signed in to change notification settings

cskonopka/twilio-flex-AzureIoT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Twilio Flex Azure IoT

Instructions

  1. Install all dependencies by running:
npm install
  1. Copy webchat-appConfig.sample.js in public/assets folder and configure accordingly to use your Twilio account
cp public/assets/webchat-appConfig.sample.js public/assets/webchat-appConfig.js
  1. Start Flex UI by running:
npm start
  1. Build project for production:
npm build

Workflow

Flex -> Accept incoming SMS -> Twilio Studio HTTP POST -> Azure Function (Create Programmable Wireless Command in Node.js) -> Receive Command with Arduino MKR 1400 -> Blink on-board LED

Twilio Flex

Flex agent receives a new SMS prompt and when a new chat starts an HTTP POST is routed in a Twilio Studio Flow.

Twilio Studio

When the Flex agent accepts a new incoming SMS, it routes to the Twilio Studio Flow named "Messaging Flow". A trigger is generated by the "Task Created" outlet and the output routes to an HTTP POST Request, executing an Azure Function from Studio.

Azure Function

The Azure Function creates a new Programmable Wireless Command using Node.js. More information about how to create an Azure Function using the Azure CLI can found on the Microsoft website.

module.exports = async function (context, req) {
    const accountSid = 'TWILIO_ACCOUNT_SID';
    const authToken = 'TWILIO_AUTH';
    const client = require('twilio')(accountSid, authToken);

    client.wireless.commands
               .create({sim: 'SIM_SSID', command: '1'})
               .then(command => console.log(command.sid));
};

Arduino MKR 1400

The Arduino MKR 1400 receives a Command created by the Azure Function, filters the data and then illuminates the on-board LED to notify the agent that an SMS conversation has started.

#include <MKRGSM.h>

GSM gsmAccess;
GSM_SMS sms;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("SMS Messages Receiver");
  boolean connected = false;
  gsmAccess.begin();
  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

void loop() {
  int c;
  if (sms.available()) {
    Serial.println("Message received from:");
    if (sms.peek() == '#') {
      Serial.println("Discarded SMS");
      sms.flush();
    }
    while ((c = sms.read()) != -1) {
      if (c == '1') {
        Serial.println("woo!");
        // Turn on LED
        digitalWrite(LED_BUILTIN, HIGH);
        delay(5000);
        // Turn off LED
        digitalWrite(LED_BUILTIN, LOW);
        delay(3000);
      } else if (c == '0') {
        Serial.println("sad!");
      } else {
        Serial.println("incorrect message");
      }
    }
    Serial.println("\nEND OF MESSAGE");
    sms.flush();
    Serial.println("MESSAGE DELETED");
  }
  delay(1000);
}

About

Example of how to connect an incoming SMS in Flex to a cellular IoT device using an Azure Function.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published