为什么我的Alexa技能套件意图超时

时间:2019-10-28 16:55:41

标签: promise async-await alexa alexa-skills-kit

使用Alexa技能套件节点模块。我正在尝试使用Alexa技能套件从处理函数中进行MQTT调用。看来正确地发送了消息,兑现了承诺,但是当我测试它时,实际意图却超时了。我多次查看了代码,但无法弄清楚。

    const NextPatternIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'NextPatternIntent';
    },
    async handle(handlerInput) {
        const speakOutput = handlerInput.t('NEXT_MSG');
        const response = await sendMQTT();
        console.log(response);

        return handlerInput.responseBuilder
            .speak("Okay updated the pattern")
            .getResponse();
    }
    };

    function sendMQTT() {
    return new Promise(((resolve, reject) => {
        var client  = mqtt.connect('mqtt://broker.mqtt-dashboard.com')
        client.on('connect', function () {
            console.log("connected to mqtt server");
            client.subscribe('prancerInTopic', function (err) {
                if (!err) {
                    console.log("subscribed, sending message");
                    client.publish('mytopic', '{"event":"nextpattern"}');
                    resolve("it worked");
                }
                else
                {
                    console.log(err);
                    reject(err);
                }
            })
        })
    }));
}

0 个答案:

没有答案