2017-09-26 12 views
0

THIS標準ダイレクトラインサンプルを実行しようとしています。 Directlineのウェブサイトでキーを生成して入力しました。このエラーが発生し続けるdirectline APIでclient.clientAuthorizations.addが定義されていません

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

client.clientAuthorizations APIは(hereを参照)Swagger 3.xではコンストラクタベースのAPIに置き換えられました。

あなたは2.xSwagger-JS依存関係をダウングレードまたはSwagger 3.x API

にコードを更新しますか
関連する問題