1

我正在尝试使用 Azure Bot Service 来创建 LINE@ 聊天机器人。下面是 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() }
}
4

0 回答 0