Skip to content

Commit

Permalink
share MockBot and DirectLine interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
danmarshall committed Jul 13, 2017
1 parent 18a01ee commit e573a74
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
4 changes: 3 additions & 1 deletion test/commands_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as server_content from './server_content';
import * as dl from "../node_modules/botframework-directlinejs/built/directLine";
import * as Nightmare from 'nightmare';
import * as express from 'express';
import { MockBot } from "./mock_dl/index";

declare let module: any;

interface ISendActivity {
Expand Down Expand Up @@ -219,7 +221,7 @@ var commands_map: CommandValuesMap = {
server: function (res, sendActivity) {
sendActivity(res, {
type: "message",
from: server_content.bot,
from: MockBot,
timestamp: new Date().toUTCString(),
channelId: "webchat",
textFormat: "markdown",
Expand Down
66 changes: 32 additions & 34 deletions test/mock_dl/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
require('dotenv').config();

import * as dl from "../../node_modules/botframework-directlinejs/built/directLine";
import * as express from 'express';
import bodyParser = require('body-parser');
import * as path from 'path';
import * as fs from 'fs';

export var MockBot: dl.User = {
id: "mockbot",
name: "MockBot"
}

const app = express();

app.use(bodyParser.json()); // for parsing application/json
Expand Down Expand Up @@ -47,7 +53,8 @@ app.post('/mock/tokens/generate', (req, res) => {
res.send({
conversationId,
token,
expires_in
expires_in,
timestamp: new Date().toUTCString(),
});
});

Expand All @@ -57,13 +64,14 @@ app.post('/mock/tokens/refresh', (req, res) => {
res.send({
conversationId,
token,
expires_in
expires_in,
timestamp: new Date().toUTCString()
});
});

let counter: number;
let messageId: number;
let queue: Activity[];
let queue: dl.Activity[];

app.post('/mock/conversations', (req, res) => {
counter = 0;
Expand Down Expand Up @@ -91,35 +99,18 @@ const startConversation = (req: express.Request, res: express.Response) => {
conversationId,
token,
expires_in,
streamUrl
streamUrl,
timestamp: new Date().toUTCString()
});
sendMessage(res, `Welcome to MockBot! Here is test ${test} on area ${area}`);
}

interface Activity {
type: string,
timestamp?: string,
textFormat?: string,
text?: string,
channelId?: string,
attachmentLayout?: string,
attachments?: Attachment,
id?: string,
from?: { id?: string, name?: string }
}

interface Attachment {

}

const sendMessage = (res: express.Response, text: string) => {
queue.push({
sendActivity(res, {
type: "message",
text
})
text: "Welcome to MockBot!",
timestamp: new Date().toUTCString(),
from: MockBot
});
}

const sendActivity = (res: express.Response, activity: Activity) => {
const sendActivity = (res: express.Response, activity: dl.Activity) => {
queue.push(activity)
}

Expand All @@ -146,13 +137,14 @@ const postMessage = (req: express.Request, res: express.Response) => {
const id = messageId++;
res.send({
id,
timestamp: new Date().toUTCString()
});
processCommand(req, res, req.body.text, id);
}

const printCommands = () => {
let cmds = "### Commands\r\n\r\n";
for(var command in commands){
for (var command in commands) {
cmds += `* ${command}\r\n`;
}
return cmds;
Expand Down Expand Up @@ -187,7 +179,8 @@ const processCommand = (req: express.Request, res: express.Response, cmd: string
type: "message",
timestamp: new Date().toUTCString(),
channelId: "webchat",
text: printCommands()
text: printCommands(),
from: MockBot
});
return;
case 'end':
Expand All @@ -204,7 +197,8 @@ const processCommand = (req: express.Request, res: express.Response, cmd: string
type: "message",
timestamp: new Date().toUTCString(),
channelId: "webchat",
text: "echo: " + req.body.text
text: "echo: " + req.body.text,
from: MockBot
});
}
return;
Expand All @@ -213,7 +207,8 @@ const processCommand = (req: express.Request, res: express.Response, cmd: string
type: "message",
timestamp: new Date().toUTCString(),
channelId: "webchat",
text: "echo: " + req.body.text
text: "echo: " + req.body.text,
from: MockBot
});
return;
}
Expand Down Expand Up @@ -244,6 +239,7 @@ const upload = (req: express.Request, res: express.Response) => {
const id = messageId++;
res.send({
id,
timestamp: new Date().toUTCString()
});
}

Expand Down Expand Up @@ -275,12 +271,14 @@ const getMessages = (req: express.Request, res: express.Response) => {
msg.from = { id: "id", name: "name" };
res.send({
activities: [msg],
watermark: id
watermark: id,
timestamp: new Date().toUTCString()
});
} else {
res.send({
activities: [],
watermark: messageId
watermark: messageId,
timestamp: new Date().toUTCString()
})
}
}
Expand Down
6 changes: 1 addition & 5 deletions test/server_content.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import * as dl from "../node_modules/botframework-directlinejs/built/directLine";
import { MockBot as bot } from "./mock_dl/index";
const config = require('./mock_dl_server_config');
const asset_url = "http://localhost:" + config["port"] + "/assets/";

export var bot: dl.User = {
id: "bot",
name: "botname"
}

/*
* Function that renders Adaptive Cards
*
Expand Down

0 comments on commit e573a74

Please sign in to comment.