Twilio Studio: Forward SMS conversation log to email

邮差的信 提交于 2021-01-28 09:20:55

问题


I'm using SMS studio do have a quick chat bot conversation with inbound SMS messages, and would like to forward the conversation log to email after it's complete. I've written a function that uses the SendGrid API to forward SMSes to email. It works independently - ie, if I configure the phone number to run the function immediately as a text comes in, it will email that single SMS input.

However, I'd like to add the function to the end of of Twilio Studio flow, so that it emails the entire log of the conversation to me, once it's over. Once I append the function to the end of the studio flow, it stops working, and I get a failure notice.

Here's the code in the function:

const got = require('got');

exports.handler = function(context, event, callback) 
{
    const requestBody = {
    personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
    from: { email: context.FROM_EMAIL_ADDRESS },
    subject: `New SMS message from: ${event.From}`,
    content: [
      {
        type: 'text/plain',
        value: event.Body
      }
    ]
}

  got.post('https://api.sendgrid.com/v3/mail/send', {
    headers: {
      Authorization: `Bearer ${context.SENDGRID_API_KEY}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(requestBody)
  })
    .then(response => {
      let twiml = new Twilio.twiml.MessagingResponse();
      callback(null, twiml);
    })
    .catch(err => {
      callback(err);
    });

};`

Here's the error the debugger returns, if I make this function the last step in a Twilio studio flow:

Error - 81017
Error on Twilio Function response
There was an error in the response back from a Twilio Function attached to the Studio flow.
Possible Causes
Your Function timed out before responding
Your Function returned an error response
Possible Solutions
Your Function must contain a callback.
Make sure you place the Function callback callback(err, response) is placed correctly in your Function code.
If you are using a JavaScript promise, make sure the callback is called in both success and catch blocks.
Your Function responded with an error.

I'm having a hard time figuring out what the error is. Does anyone have any tips?

Thanks!


回答1:


Jarod from Twilio. I actually wrote an app very similar to this. Code looks good to me. Often when people encounter an error of this nature it is coming from the SendGrid promise. Here are two tips:

  1. You can actually log errors and view responses from the function if you leave the function open while testing it. The logs will be at the bottom. These usually have more information.
  2. Check that you are using an outbound email address that has been whitelisted on your SendGrid account Whitelabel - Sendgrid
  3. Make sure you have added your environment variables to your Functions config. Twilio Functions Configuration - Console

Hope that helps! If not feel free to email support@twilio.com with more questions.



来源:https://stackoverflow.com/questions/52481378/twilio-studio-forward-sms-conversation-log-to-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!