2018-05-07 20:45:11 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
|
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| ( ) |( (_) ) |
|
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| Licensed under the AGPL-3.0. |
|
|
|
|
| |
|
|
|
|
| According to our dual licensing model, this program can be used either |
|
|
|
|
| under the terms of the GNU Affero General Public License, version 3, |
|
|
|
|
| or under a proprietary license. |
|
|
|
|
| |
|
|
|
|
| The texts of the GNU Affero General Public License with an additional |
|
|
|
|
| permission and of our proprietary license can be found at and |
|
|
|
|
| in the LICENSE file you have received along with this program. |
|
|
|
|
| |
|
|
|
|
| This program is distributed in the hope that it will be useful, |
|
2018-09-11 19:40:53 -03:00
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
2018-05-07 20:45:11 -03:00
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
|
|
|
| "General Bots" is a registered trademark of Pragmatismo.io. |
|
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
const Path = require("path")
|
|
|
|
const Fs = require("fs")
|
|
|
|
const _ = require("lodash")
|
|
|
|
const Parse = require("csv-parse")
|
|
|
|
const Async = require("async")
|
|
|
|
const UrlJoin = require("url-join")
|
|
|
|
const Walk = require("fs-walk")
|
|
|
|
const logger = require("../../../src/logger")
|
|
|
|
const Swagger = require('swagger-client')
|
|
|
|
const rp = require('request-promise')
|
|
|
|
import { GBService } from "botlib"
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2018-05-11 23:27:00 -03:00
|
|
|
export class ConsoleDirectLine extends GBService {
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
pollInterval = 1000
|
|
|
|
directLineSecret = ''
|
|
|
|
directLineClientName = 'DirectLineClient'
|
|
|
|
directLineSpecUrl = 'https://docs.botframework.com/en-us/restapi/directline3/swagger.json'
|
2018-05-07 20:45:11 -03:00
|
|
|
|
|
|
|
constructor(directLineSecret) {
|
2018-09-11 19:40:53 -03:00
|
|
|
super()
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
this.directLineSecret = directLineSecret
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2018-05-11 22:18:38 -03:00
|
|
|
|
|
|
|
// TODO: Migrate to Swagger 3.
|
2018-05-07 20:45:11 -03:00
|
|
|
let directLineClient = rp(this.directLineSpecUrl)
|
|
|
|
.then(function (spec) {
|
|
|
|
return new Swagger({
|
|
|
|
spec: JSON.parse(spec.trim()),
|
|
|
|
usePromise: true
|
2018-09-11 19:40:53 -03:00
|
|
|
})
|
2018-05-07 20:45:11 -03:00
|
|
|
})
|
|
|
|
.then(function (client) {
|
|
|
|
client.clientAuthorizations.add('AuthorizationBotConnector',
|
2018-09-11 19:40:53 -03:00
|
|
|
new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + directLineSecret, 'header'))
|
|
|
|
return client
|
2018-05-07 20:45:11 -03:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2018-09-11 19:40:53 -03:00
|
|
|
console.error('Error initializing DirectLine client', err)
|
|
|
|
})
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2018-05-11 22:18:38 -03:00
|
|
|
// TODO: Remove *this* issue.
|
2018-09-11 19:40:53 -03:00
|
|
|
let _this_ = this
|
2018-09-09 16:18:26 -03:00
|
|
|
directLineClient.then((client)=> {
|
2018-05-11 22:18:38 -03:00
|
|
|
client.Conversations.Conversations_StartConversation()
|
2018-05-07 20:45:11 -03:00
|
|
|
.then(function (response) {
|
2018-09-11 19:40:53 -03:00
|
|
|
return response.obj.conversationId
|
2018-05-12 22:36:12 -03:00
|
|
|
})
|
2018-05-07 20:45:11 -03:00
|
|
|
.then(function (conversationId) {
|
2018-09-11 19:40:53 -03:00
|
|
|
_this_.sendMessagesFromConsole(client, conversationId)
|
|
|
|
_this_.pollMessages(client, conversationId)
|
2018-05-07 20:45:11 -03:00
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2018-09-11 19:40:53 -03:00
|
|
|
console.error('Error starting conversation', err)
|
|
|
|
})
|
|
|
|
})
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
sendMessagesFromConsole(client, conversationId) {
|
2018-09-11 19:40:53 -03:00
|
|
|
let _this_ = this
|
|
|
|
process.stdin.resume()
|
|
|
|
var stdin = process.stdin
|
|
|
|
process.stdout.write('Command> ')
|
2018-05-07 20:45:11 -03:00
|
|
|
stdin.addListener('data', function (e) {
|
2018-09-11 19:40:53 -03:00
|
|
|
var input = e.toString().trim()
|
2018-05-07 20:45:11 -03:00
|
|
|
if (input) {
|
|
|
|
// exit
|
|
|
|
if (input.toLowerCase() === 'exit') {
|
2018-09-11 19:40:53 -03:00
|
|
|
return process.exit()
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
client.Conversations.Conversations_PostActivity(
|
|
|
|
{
|
|
|
|
conversationId: conversationId,
|
|
|
|
activity: {
|
|
|
|
textFormat: 'plain',
|
|
|
|
text: input,
|
|
|
|
type: 'message',
|
|
|
|
from: {
|
2018-05-12 22:36:12 -03:00
|
|
|
id: _this_.directLineClientName,
|
|
|
|
name: _this_.directLineClientName
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
2018-09-11 19:40:53 -03:00
|
|
|
console.error('Error sending message:', err)
|
|
|
|
})
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
process.stdout.write('Command> ')
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
2018-09-11 19:40:53 -03:00
|
|
|
})
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
|
2018-05-11 22:18:38 -03:00
|
|
|
/** TBD: Poll Messages from conversation using DirectLine client */
|
2018-05-07 20:45:11 -03:00
|
|
|
pollMessages(client, conversationId) {
|
2018-09-11 19:40:53 -03:00
|
|
|
let _this_ = this
|
|
|
|
console.log('Starting polling message for conversationId: ' + conversationId)
|
|
|
|
var watermark = null
|
2018-05-07 20:45:11 -03:00
|
|
|
setInterval(function () {
|
|
|
|
client.Conversations.Conversations_GetActivities({ conversationId: conversationId, watermark: watermark })
|
|
|
|
.then(function (response) {
|
2018-09-11 19:40:53 -03:00
|
|
|
watermark = response.obj.watermark // use watermark so subsequent requests skip old messages
|
|
|
|
return response.obj.activities
|
2018-05-07 20:45:11 -03:00
|
|
|
})
|
2018-09-11 19:40:53 -03:00
|
|
|
.then(_this_.printMessages, _this_.directLineClientName)
|
|
|
|
}, this.pollInterval)
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
|
2018-05-11 22:18:38 -03:00
|
|
|
printMessages(activities, directLineClientName) {
|
|
|
|
|
2018-05-07 20:45:11 -03:00
|
|
|
if (activities && activities.length) {
|
|
|
|
// ignore own messages
|
2018-09-11 19:40:53 -03:00
|
|
|
activities = activities.filter(function (m) { return m.from.id !== directLineClientName })
|
2018-05-07 20:45:11 -03:00
|
|
|
|
|
|
|
if (activities.length) {
|
|
|
|
|
|
|
|
// print other messages
|
2018-05-11 22:18:38 -03:00
|
|
|
activities.forEach(activity => {
|
2018-09-11 19:40:53 -03:00
|
|
|
console.log(activity.text)
|
|
|
|
}, this)
|
2018-05-07 20:45:11 -03:00
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
process.stdout.write('Command> ')
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printMessage(activity) {
|
|
|
|
if (activity.text) {
|
2018-09-11 19:40:53 -03:00
|
|
|
console.log(activity.text)
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (activity.attachments) {
|
|
|
|
activity.attachments.forEach(function (attachment) {
|
|
|
|
switch (attachment.contentType) {
|
|
|
|
case "application/vnd.microsoft.card.hero":
|
2018-09-11 19:40:53 -03:00
|
|
|
this.renderHeroCard(attachment)
|
|
|
|
break
|
2018-05-07 20:45:11 -03:00
|
|
|
|
|
|
|
case "image/png":
|
2018-09-11 19:40:53 -03:00
|
|
|
console.log('Opening the requested image ' + attachment.contentUrl)
|
|
|
|
open(attachment.contentUrl)
|
|
|
|
break
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
2018-09-11 19:40:53 -03:00
|
|
|
})
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderHeroCard(attachment) {
|
2018-09-11 19:40:53 -03:00
|
|
|
var width = 70
|
2018-05-07 20:45:11 -03:00
|
|
|
var contentLine = function (content) {
|
|
|
|
return ' '.repeat((width - content.length) / 2) +
|
|
|
|
content +
|
2018-09-11 19:40:53 -03:00
|
|
|
' '.repeat((width - content.length) / 2)
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
|
2018-09-11 19:40:53 -03:00
|
|
|
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) + '/')
|
2018-05-07 20:45:11 -03:00
|
|
|
}
|
|
|
|
}
|