client.clientAuthorizations.add在直线API

时间:2017-09-26 06:30:34

标签: swagger botframework direct-line-botframework

我正在尝试运行THIS标准直接线样本。我在Directline网站上生成了密钥并输入了它。我一直收到这个错误

Error initializing DirectLine client TypeError: Cannot read property 'add' of undefined
    at D:\bots\zupcoin test\twitter\directline_client.js:21:36
    at tryCatcher (D:\bots\zupcoin test\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (D:\bots\zupcoin test\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (D:\bots\zupcoin test\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (D:\bots\zupcoin test\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (D:\bots\zupcoin test\node_modules\bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (D:\bots\zupcoin test\node_modules\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (D:\bots\zupcoin test\node_modules\bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (D:\bots\zupcoin test\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:781:20)
    at tryOnImmediate (timers.js:743:5)
    at processImmediate [as _immediateCallback] (timers.js:714:5)
Unhandled rejection TypeError: Cannot read property 'Conversations' of undefined
    at D:\bots\zupcoin test\twitter\directline_client.js:30:11
    at tryCatcher (D:\bots\zupcoin test\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (D:\bots\zupcoin test\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (D:\bots\zupcoin test\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (D:\bots\zupcoin test\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (D:\bots\zupcoin test\node_modules\bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (D:\bots\zupcoin test\node_modules\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (D:\bots\zupcoin test\node_modules\bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (D:\bots\zupcoin test\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:781:20)
    at tryOnImmediate (timers.js:743:5)
    at processImmediate [as _immediateCallback] (timers.js:714:5)\

这是我的完整代码,带有一个虚拟键,用于说明目的。我没有修改示例中的任何内容。 client.clientAuthorizations为null,任何建议,提前谢谢

var Swagger = require('swagger-client');
var open = require('open');
var rp = require('request-promise');

// config items
var pollInterval = 1000;
var directLineSecret = 'hjaifnkghsfsgjsgfiagfhsgojisaasoigsohjgjpfsgdsghsfasf';
var directLineClientName = 'Twitter';
var directLineSpecUrl = 'https://docs.botframework.com/en-us/restapi/directline3/swagger.json';

var directLineClient = rp(directLineSpecUrl)
    .then(function (spec) {
        // client
        return new Swagger({
            spec: JSON.parse(spec.trim()),
            usePromise: true
        });
    })
    .then(function (client) {
        // add authorization header to client
        client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + directLineSecret, 'header'));
        return client;
    })
    .catch(function (err) {
        console.error('Error initializing DirectLine client', err);
    });

// once the client is ready, create a new conversation 
directLineClient.then(function (client) {
    client.Conversations.Conversations_StartConversation()                          // create conversation
        .then(function (response) {
            return response.obj.conversationId;
        })                            // obtain id
        .then(function (conversationId) {
            sendMessagesFromConsole(client, conversationId);                        // start watching console input for sending new messages to bot
            pollMessages(client, conversationId);                                   // start polling messages from bot
        });
});

// Read from console (stdin) and send input to conversation using DirectLine client
function sendMessagesFromConsole(client, conversationId) {
    var stdin = process.openStdin();
    process.stdout.write('Command> ');
    stdin.addListener('data', function (e) {
        var input = e.toString().trim();
        if (input) {
            // exit
            if (input.toLowerCase() === 'exit') {
                return process.exit();
            }

            // send message
            client.Conversations.Conversations_PostActivity(
                {
                    conversationId: conversationId,
                    activity: {
                        textFormat: 'plain',
                        text: input,
                        type: 'message',
                        from: {
                            id: directLineClientName,
                            name: directLineClientName
                        }
                    }
                }).catch(function (err) {
                    console.error('Error sending message:', err);
                });

            process.stdout.write('Command> ');
        }
    });
}

// Poll Messages from conversation using DirectLine client
function pollMessages(client, conversationId) {
    console.log('Starting polling message for conversationId: ' + conversationId);
    var watermark = null;
    setInterval(function () {
        client.Conversations.Conversations_GetActivities({ conversationId: conversationId, watermark: watermark })
            .then(function (response) {
                watermark = response.obj.watermark;                                 // use watermark so subsequent requests skip old messages 
                return response.obj.activities;
            })
            .then(printMessages);
    }, pollInterval);
}

// Helpers methods
function printMessages(activities) {
    if (activities && activities.length) {
        // ignore own messages
        activities = activities.filter(function (m) { return m.from.id !== directLineClientName });

        if (activities.length) {
            process.stdout.clearLine();
            process.stdout.cursorTo(0);

            // print other messages
            activities.forEach(printMessage);

            process.stdout.write('Command> ');
        }
    }
}

function printMessage(activity) {
    if (activity.text) {
        console.log(activity.text);
    }

    if (activity.attachments) {
        activity.attachments.forEach(function (attachment) {
            switch (attachment.contentType) {
                case "application/vnd.microsoft.card.hero":
                    renderHeroCard(attachment);
                    break;

                case "image/png":
                    console.log('Opening the requested image ' + attachment.contentUrl);
                    open(attachment.contentUrl);
                    break;
            }
        });
    }
}

function renderHeroCard(attachment) {
    var width = 70;
    var contentLine = function (content) {
        return ' '.repeat((width - content.length) / 2) +
            content +
            ' '.repeat((width - content.length) / 2);
    }

    console.log('/' + '*'.repeat(width + 1));
    console.log('*' + contentLine(attachment.content.title) + '*');
    console.log('*' + ' '.repeat(width) + '*');
    console.log('*' + contentLine(attachment.content.text) + '*');
    console.log('*'.repeat(width + 1) + '/');
}

1 个答案:

答案 0 :(得分:2)

client.clientAuthorizations API已替换为Swagger 3.x中基于构造函数的API(请参阅here)。

您可以将Swagger-JS依赖关系降级为2.x或将代码更新为Swagger 3.x API