0

We have a studio flow called "Google LA" that's triggered via Rest API. This flow has a Send and Wait for Reply so we hook this flow to "When a message comes in" so it will follow the rest of the flow when customer rates the service 1 to 5 stars. Now, within the Send and Wait for Reply, we want the customer's reply be forwarded to our main business phone number for tracking/recording purposes and so we can address their issues for rating us 1 to 3 stars. Here's our setup:

enter image description here

This is what we want:

enter image description here

Edited for philnash suggestion:

I created a function in Twilio with this code:

exports.handler = function(context, event, callback) {

    const accountSid = context.ACCOUNT_SID;
    const authToken = context.AUTH_TOKEN;
    const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
     body: widgets.negative1_3.inbound.Body,
     from: '+12132779513',
     to: '+12133885256'
   })
  .then(message => console.log(message.sid));
  

};

However, it did not send anything or the customer response. I renamed the negative1-3 widget to negative1_3 and published the studio flow.

enter image description here

I tried changing the body: 'Hello' to make sure that my function works, and yes. I received the 'Hello' sms to my verified caller ID phone number after it reaches the first_question -> check_response -> negative1_3.

geeksified
  • 61
  • 5

1 Answers1

1

Twilio developer evangelist here.

You don't necessarily need to forward the message here. You can make an API call to your own service with all the data you need from the message, so you can store and react to the information that way.

To do so you will want to add either an HTTP Request widget or a Run Function widget after the Send and Wait For Reply widget. Within those widgets, you can access the reply from the Send And Wait For Reply widget using liquid tags. You can see how to call on the variables in the docs for the Send and Wait For Reply widget. In the case of your widget, you should be able to get the body of the reply by referring to:

widgets.negative1-3.inbound.Body

(Although I am not sure how the name "negative1-3" will work, so you might try widgets["negative1-3"] instead, or rename the widget with underscores.)

Using the body of the inbound message, as well as the from number, you can send the data to your own application with the HTTP request widget or with a Run Function widget.

Edit

Your function can only access parameters that you set in the function widget config. You can then access those parameters in the event object. You also need to return once the message is sent successfully using the callback function. One other tip, you don't need to instantiate your own client, you can get it from the context. Like so:

exports.handler = function(context, event, callback) {

  const client = context.getTwilioClient();

  client.messages
    .create({
       body: event.Body,
       from: '+12132779513',
       to: '+12133885256'
     })
    .then(message => {
      console.log(message.sid);
      callback(null, "OK");
    })
    .catch(error => callback(error));
};
philnash
  • 70,667
  • 10
  • 60
  • 88
  • Hi @philnash. Thank you. I am not receiving any response from the Send & Wait for Reply, which I also changed from negative1-3 to negative1_3. I tried setting up this way: `client.messages .create({ body: widgets.negative1_3.inbound.Body, from: '+12132779513', to: '+12133885256' }) .then(message => console.log(message.sid));` – geeksified Jun 28 '21 at 07:08
  • 1
    This raises some questions! Does your studio flow correctly move past the `first_question` widget? Does it succeed both when you start it via REST API and via inbound message? How have you set up the code in your comment? Is that in a Twilio Function? – philnash Jun 28 '21 at 07:12
  • Hi philnash. Yes it did pass the first_question and straight to negative1_3. I even setup the `body: 'Hello', from: 'twilio phone here', to: 'my verified caller id'` and that above, I received the "Hello" sms to my verified caller ID. Edit: I edited my post above for reference about the whole Twilio function code. – geeksified Jun 28 '21 at 07:18
  • 1
    You cannot access the `widgets` object directly in your Function, that is only available to the Studio widgets. When you call the function you need to send the parameters into the function using the widget config. You can [set keys and values in the widget config like in this screenshot](https://i.stack.imgur.com/HLsDt.png). Then you access those parameters through the `event` object in your Function. If you set a parameter called `Body` you can access it in the function as `event.Body`. – philnash Jun 28 '21 at 07:41
  • Awesome! It works perfectly! I can now receive the body and send it to our main phone number! However, I cannot make the last widget to work: `r1_response` I did not receive that one even though I received the body using event.Body. It should be a success, right? – geeksified Jun 28 '21 at 07:48
  • 1
    Oh, that is because you are not returning a response from your Function. I've updated my answer with an update to your code to make it work. – philnash Jun 28 '21 at 07:57
  • Awesome! I now got that r1_response after returning a response from my function! Thanks again! – geeksified Jun 28 '21 at 08:04
  • If I forward this to our business email instead of forwarding to our phone number, do you have the Twilio docs I can check? I have this link but I haven't tried this yet: https://www.twilio.com/blog/2017/07/forward-incoming-sms-messages-to-email-with-node-js-sendgrid-and-twilio-functions.html – geeksified Jun 28 '21 at 08:08
  • I would strongly suggest you check the docs and try things first rather than ask people on Stack Overflow to find documentation for you. Then if you have a problem, ask a question here. Also, I wrote that blog post, so it should work for what you want. Though it was written a while back, so I'd probably upgrade the dependencies and look out for any differences there. – philnash Jun 28 '21 at 09:01