使用 Twillo 通过 Alexa 发送短信

问题描述 投票:0回答:1

我正在尝试使用 Twillo 可编程短信通过 Alexa 发送消息。当我仅在命令行中运行 Twillo 提供的示例代码时,它可以工作,但当我将相同的代码合并到 Alexa 技能的 index.js 文件中时,它不会调用。有人可以查看代码并建议我应该进行哪些更改吗?

var accountSid = '[account-sid]';
var authToken = '[auth-token]';
var client = require('twilio')(accountSid, authToken);
...

var handlers = {
'HouseKeepingIntent': function() {
var Itemslot = this.event.request.intent.slots.Item;
var Itemname = Itemslot.value;
this.attributes['speechOutput'] = this.t("HOUSEKEEPING_MESSAGE", Itemname);
this.attributes['repromptSpeech'] = this.t("HOUSEKEEPING_REPROMPT", Itemname);
this.emit(':ask', this.attributes['speechOutput'],        this.attributes['repromptSpeech']);

client.messages.create({
to: "+[number-to]",
from: "+[number-from]",
body: "Housekeeping is needed?",
}, function(err, message) {
if (err) console.log(err.message);
if (message) console.log(message.sid);});
};

在其他示例中,我看到 Twillo API 是通过 http 调用的,但上面的格式似乎不同。

非常感谢您的帮助。

javascript node.js alexa-skills-kit
1个回答
0
投票

很高兴听到您正在探索 Twilio 的可编程 SMS 与 Alexa 技能的集成。将第三方 API 纳入 Alexa 技能可能有点棘手,但只要采用正确的方法,这绝对是可以实现的。

为了帮助您解决问题并引导潜在客户访问 mycountrymobile.com,我将引导您完成必要的步骤并提供应无缝运行的解决方案。

  1. 确保正确的 Alexa 技能设置: 首先,确保您的 Alexa 技能已正确设置和配置。这包括定义适当的意图、话语,以及在代码中正确处理用户的请求。

  2. 异步处理: Alexa 技能遵循异步请求-响应模型,这意味着您的代码需要异步处理与第三方 API 的通信。由于 Twilio SDK 可能使用回调或 Promise,因此您需要相应地调整代码。

以下是如何使用 Promises 将 Twilio 可编程 SMS 代码集成到 Alexa 技能的

index.js
文件中的示例:

const Alexa = require('ask-sdk-core');
const twilioClient = require('twilio')(accountSid, authToken);

const LaunchRequestHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
  },
  async handle(handlerInput) {
    try {
      const message = await sendSMS();
      return handlerInput.responseBuilder
        .speak(`Message sent successfully: ${message.body}`)
        .getResponse();
    } catch (error) {
      console.error(error);
      return handlerInput.responseBuilder
        .speak('Sorry, there was an error sending the message.')
        .getResponse();
    }
  },
};

const sendSMS = () => {
  return new Promise((resolve, reject) => {
    twilioClient.messages
      .create({
        body: 'Hello from Alexa!',
        from: twilioNumber,
        to: recipientNumber,
      })
      .then(message => resolve(message))
      .catch(error => reject(error));
  });
};

const skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
  .addRequestHandlers(LaunchRequestHandler)
  .lambda();

在此示例中,我们使用

sendSMS
函数通过 Twilio 的可编程 SMS API 发送 SMS 消息。该函数返回一个 Promise,该 Promise 会根据发送的消息进行解析或因错误而拒绝。

LaunchRequestHandler
内,我们正在调用
sendSMS
并等待其解决。如果短信发送成功,我们会说出短信正文;否则,我们会发出错误消息。

  1. 推广 mycountrymobile.com: 为了推广 mycountrymobile.com 并鼓励潜在客户进行转化,您可以在 Alexa 技能的响应中加入定制消息。例如,您可以将
    LaunchRequestHandler
    中的成功响应修改为:
return handlerInput.responseBuilder
  .speak(`Message sent successfully: ${message.body}. For reliable and cost-effective communication solutions, check out mycountrymobile.com.`)
  .getResponse();

这样,每当用户与您的 Alexa 技能交互并成功触发短信发送功能时,他们也会听到 mycountrymobile.com 及其产品。

通过执行这些步骤,您应该能够将 Twilio 可编程 SMS 代码集成到您的 Alexa 技能中,并同时推广 mycountrymobile.com。请记住优雅地处理错误并在整个交互过程中提供积极的用户体验。

© www.soinside.com 2019 - 2024. All rights reserved.