简体   繁体   English

如何从 Twilio Function 内部重定向到工作室流程?

[英]How do I redirect to a studio flow from inside a Twilio Function?

I have a serverless function that is executed when an user sends a message through whatsapp, if the user sends a fixed location, the function get it's latitude and longitude and returns an object on the callback with the location info.我有一个无服务器 function,当用户通过 whatsapp 发送消息时执行,如果用户发送固定位置,function 获取它的纬度和经度并返回带有位置信息回调的 ZA8CFDE6331BD59EB2AC96F8。 I want to make the function run and redirect to a studio flow that is integrated with flex through proxy so I can handle the user's location there and eventually send to a flex agent.我想让 function 运行并重定向到通过代理与 flex 集成的工作室流程,这样我就可以在那里处理用户的位置并最终发送到 flex 代理。

This is the function I have:这是我拥有的 function:

exports.handler = function(context, event, callback) {
  console.log("lat: ", event.Latitude);
  console.log("lon: ", event.Longitude);

  if (!event.Latitude || !event.Longitude) {
    callback(null, {
      lat: null,
      lon: null
    });
  } else {
    const location = {
      lat: event.Latitude,
      lon: event.Longitude
    };
    
    callback(null, location);
  }
};

I'm using WhatsApp Sandbox with the above function being called on "when a message comes in".我正在使用 WhatsApp 沙盒,上面的 function 在“收到消息时”被调用。 After the function executes I want it to redirect to a Studio Flow (inside the function), is that possible?在 function 执行后,我希望它重定向到 Studio Flow(在函数内部),这可能吗? How can I do it?我该怎么做? I'm a beginner dev and new to Twilio.我是一名初学者开发人员,是 Twilio 的新手。

It's technically not a redirect but it is possible to initiate a Studio flow from a function.从技术上讲,它不是重定向,但可以从 function 启动 Studio 流。 In this case you need to pass the number of the original sender to the Studio flow via the following code snippet:在这种情况下,您需要通过以下代码段将原始发件人的号码传递给 Studio 流程:


exports.handler = function (context, event, callback) {
  const client = context.getTwilioClient();

  client.studio.v2.flows('<Flow ID>')
    .executions
    .create({ to: event.From, from: MY_NUMBER })
    .then(execution => {
      return callback(null, "hello"); // You could return something else as well
    }
};

And you can find the ID of your flow in the console (or when you click on the root element and check the Flow Configuration ):您可以在控制台中找到您的流程 ID(或者当您单击根元素并检查Flow Configuration时): 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM