如何使用直接API API从Azure Bot服务上的Line @接收消息

时间:2017-06-08 10:17:41

标签: node.js azure botframework direct-line-botframework azure-bot-service

我正在尝试使用Azure Bot服务来创建LINE @ chat bot。以下是Azure Bot Service生成的模板index.js。据我所知,我需要使用Direct Line API从LINE APP接收消息。但是,我不知道该怎么做。

"use strict";

var builder = require("botbuilder");

var botbuilder_azure = require("botbuilder-azure");

var path = require('path');

var useEmulator = (process.env.NODE_ENV == 'development');

var connector = useEmulator ? new builder.ChatConnector() : new botbuilder_azure.BotServiceConnector({

    appId: process.env['MicrosoftAppId'],
    appPassword: process.env['MicrosoftAppPassword'],
    stateEndpoint: process.env['BotStateEndpoint'],
    openIdMetadata: process.env['BotOpenIdMetadata']
});

var bot = new builder.UniversalBot(connector);

bot.localePath(path.join(__dirname, './locale'));

bot.dialog('/', function (session) {

    session.send('You said ' + session.message.text);
});

if (useEmulator) {

    var restify = require('restify');
    var server = restify.createServer();
    server.listen(3978, function() {
        console.log('test bot endpont at http://localhost:3978/api/messages');
    });
    server.post('/api/messages', connector.listen());    
} else {
    module.exports = { default: connector.listen() }
}

在研究LINE SDK之后,我对原始代码进行了一些更改。但是,我仍然无法收到来自LINE的消息。有没有人能够帮助澄清?非常感谢您的帮助。

"use strict";
var builder = require("botbuilder");
var botbuilder_azure = require("botbuilder-azure");
var path = require('path');

var useEmulator = (process.env.NODE_ENV == 'development');

var connector = useEmulator ? new builder.ChatConnector() : new botbuilder_azure.BotServiceConnector({
    appId: process.env['MicrosoftAppId'],
    appPassword: process.env['MicrosoftAppPassword'],
    stateEndpoint: process.env['BotStateEndpoint'],
    openIdMetadata: process.env['BotOpenIdMetadata']
});

var bot = new builder.UniversalBot(connector);
bot.localePath(path.join(__dirname, './locale'));

bot.dialog('/', function (session) {
    session.send('You said ' + session.message.text);
});

if (useEmulator) {
    var restify = require('restify');
    var server = restify.createServer();
    server.listen(3978, function() {
        console.log('test bot endpont at http://localhost:3978/api/messages');
    });
    server.post('/api/messages', connector.listen());    
} else {

    const line = require('@line/bot-sdk');
    const express = require('express');

    const config = {
        channelAccessToken: 'TOKEN_HIDDEN',
        channelSecret: 'SECRET_HIDDEN'
    };

    const client = new line.Client(config);

    const app = express();

    app.post('/api/messages', line.middleware(config), (req, res) => {
        Promise
            .all(req.body.events.map(handleEvent))
            .then((result) => res.json(result));
    });

    // event handler
    function handleEvent(event) {
        if (event.type !== 'message' || event.message.type !== 'text') {
            // ignore non-text-message event
            return Promise.resolve(null);
        }

        const echo = { type: 'text', text: 'test' };

        // use reply API
        return client.replyMessage(event.replyToken, echo);
    }

    const port = process.env.PORT || 3000;

    app.listen(port, () => {
      console.log(`listening on ${port}`);
    });  

    module.exports = { default: connector.listen() }
}

0 个答案:

没有答案