我正在尝试从 Alexa 技能向 Mailgun(授权 ID)发送电子邮件通知。在节点环境中隔离启动时,代码运行良好。但是在 Lambda 上执行时发送邮件失败。
这是代码段:
var User= this.attributes['UserName']; /*session variable is working fine...*/
var data = {
from: 'xxxxxxxx', /*Mailgun sandbox*/
to: 'zzzzzzzz', /* Authorized mail ID*/
subject: 'Request',
text: `Hi, ${Alias} is waiting for you.`
};
mailgun.messages().send(data, function(error, body) {
console.log(body);
});
this.response.speak('A message has been sent to ${User}.');
this.emit(':responseReady');
编辑:我的环境变量正确设置为:
/* Parameters for MAILGUN*/
var api_key = process.env.MAILGUN_API_KEY;
var domain = process.env.MAILGUN_DOMAIN;
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});
Lambda 函数正确设置为:
exports.handle = function(event, context) { /* "index.handler" would call
exports.handler in index.js.*/
var alexa = Alexa.handler(event, context);
alexa.appId = appId;
alexa.registerHandlers(newSessionHandler, greetingHandler, aliasHandler, addressHandler, daysHandler1, accessHandler, daysHandler2, hostnameHandler, hostHandler, facilityHandler1, facilityHandler2, hsschrHandler, hsschostHandler); /* List of handlers */
alexa.execute();
};
lambda 代码执行没有问题,整个代码都被执行,并且从 Alexa 收到响应,例如“已发送消息”。但实际上并非如此。理想情况下,它应该首先发送邮件,然后发出响应。
我正在使用 Apex 进行 lambda 部署,并且我已将 project.json 中的环境变量正确设置为“环境”:
{
"MAILGUN_API_KEY": "${MAILGUN_API_KEY}",
"MAILGUN_DOMAIN": "${MAILGUN_DOMAIN}"
}