Altered the web part to use the currently logged in user's username when sending messages. Also updated the README and cleaned up some unused references in the code.

This commit is contained in:
DESKTOP-K8O6TUG\gary 2016-10-11 21:06:37 +01:00
parent 318960f2a9
commit 354704dca4
7 changed files with 27 additions and 25 deletions

View File

@ -18,4 +18,6 @@
"lib": true,
"temp": true
}
,
"typescript.check.workspaceVersion": false
}

View File

@ -1,9 +1,10 @@
# Microsoft Bot Framework Web Chat
## Summary
Short summary on functionality and used technologies.
A web part that acts as a web chat component for bot's built on the Microsoft Bot Framework using the Direct Line API. When sending messages
the web part uses the username of the currently logged in user. The web part has settings for color for branding purposes.
[picture of the web part in action]
![bot framework client web part](screenshot.png)
## Applies to
@ -12,10 +13,10 @@ Short summary on functionality and used technologies.
* [Microsoft Bot Framework](http://dev.botframework.com)
## Prerequisites
> You need to have a bot created and registered using the Microsoft Bot Framework and registered to use the Direct Line Channel,
which will give you the secret needed when adding this web part to the page. For more information on creating a bot and registering
the channel you can see the official web site at [dev.botframework.com](http://dev.botframework.com), as well as various tutorials
> You need to have a bot created and registered using the Microsoft Bot Framework and registered to use the Direct Line Channel,
which will give you the secret needed when adding this web part to the page. For more information on creating a bot and registering
the channel you can see the official web site at [dev.botframework.com](http://dev.botframework.com), as well as various tutorials
over at [www.garypretty.co.uk/category/microsoft-bot-framework/](http://www.garypretty.co.uk/category/microsoft-bot-framework/)
## Solution
@ -45,7 +46,7 @@ Version|Date|Comments
- Register your bot in the Microsoft Bot Framework Portal, configure the Direct Line channel on the bot and obtain your Direct Line secret.
## Features
## Features
This Web Part illustrates the following concepts on top of the SharePoint Framework:
- Connecting and communicating with a bot built on the Microsoft Bot Framework using the Direct Line Channel
@ -54,7 +55,7 @@ This Web Part illustrates the following concepts on top of the SharePoint Framew
- React
When adding the web part to a page you need to obtain your Bot Direct Line Channel secret via the [Bot Framework Portal](http://dev.botframework.com).
You then add this secret via the Property Pane of the web part. If there is an error initializing the Direct Line Client with the bot then they will
You then add this secret via the Property Pane of the web part. If there is an error initializing the Direct Line Client with the bot then they will
be shown in the console within the browser.
Additional settings can be set to style the web part, including;
@ -64,7 +65,7 @@ Additional settings can be set to style the web part, including;
- Placeholder text
- Foreground / background colors for messages, both from the user and from the bot
Currently this web part only supports plain text conversations with a bot. Other message types,
Currently this web part only supports plain text conversations with a bot. Other message types,
such as Rich Cards and Attachments are not supported, but are on the roadmap for a future update.
<img src="https://telemetry.sharepointpnp.com/sp-dev-fx-webparts/samples/react-bot-framework" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -21,8 +21,7 @@
"botMessagesBackgroundColor": "c8c8c8",
"botMessagesForegroundColor": "000000",
"userMessagesBackgroundColor": "c7e0f4",
"userMessagesForegroundColor": "000000",
"directLineClientName": "DirectLineClient"
"userMessagesForegroundColor": "000000"
}
}]
}

View File

@ -7,7 +7,6 @@ import {
PropertyPaneTextField
} from '@microsoft/sp-client-preview';
import * as strings from 'botFrameworkChatStrings';
import BotFrameworkChat, { IBotFrameworkChatProps } from './components/BotFrameworkChat';
import { IBotFrameworkChatWebPartProps } from './IBotFrameworkChatWebPartProps';
@ -29,7 +28,7 @@ export default class BotFrameworkChatWebPart extends BaseClientSideWebPart<IBotF
botMessagesForegroundColor: this.properties.botMessagesForegroundColor,
userMessagesBackgroundColor: this.properties.userMessagesBackgroundColor,
userMessagesForegroundColor: this.properties.userMessagesForegroundColor,
directLineClientName: this.properties.directLineClientName
context: this.context
});
ReactDom.render(element, this.domElement);
@ -47,10 +46,7 @@ export default class BotFrameworkChatWebPart extends BaseClientSideWebPart<IBotF
groupName: 'Bot Connection',
groupFields: [
PropertyPaneTextField('directLineSecret', {
label: 'Direct Line Channel Secret'
}),
PropertyPaneTextField('directLineClientName', {
label: 'Bot User Name (send messages as...)'
label: 'Direct Line Secret'
})
]
},
@ -99,9 +95,8 @@ export default class BotFrameworkChatWebPart extends BaseClientSideWebPart<IBotF
private _validateColorPropertyAsync(value: string): string {
var colorRegex = /^([a-zA-Z0-9]){6}$/;
if(!value || colorRegex.test(value) == false)
{
return "Please enter a valid 6 character hex color value";
if (!value || colorRegex.test(value) == false) {
return "Please enter a valid 6 character hex color value";
}
return "";

View File

@ -1,3 +1,5 @@
import { IWebPartContext } from '@microsoft/sp-client-preview';
export interface IBotFrameworkChatWebPartProps {
description: string;
message: string;
@ -9,5 +11,5 @@ export interface IBotFrameworkChatWebPartProps {
botMessagesForegroundColor: string;
userMessagesBackgroundColor: string;
userMessagesForegroundColor: string;
directLineClientName: string;
context: IWebPartContext;
}

View File

@ -15,6 +15,7 @@ export default class BotFrameworkChat extends React.Component<IBotFrameworkChatP
private directLineClientSwagger;
private messagesHtml;
private currentMessageText;
private sendAsUserName;
public render(): JSX.Element {
return (
@ -63,6 +64,8 @@ export default class BotFrameworkChat extends React.Component<IBotFrameworkChatP
});
});
this.sendAsUserName = this.props.context.pageContext.user.loginName;
this.printMessage = this.printMessage.bind(this);
}
}
@ -98,7 +101,7 @@ export default class BotFrameworkChat extends React.Component<IBotFrameworkChatP
{
conversationId: this.conversationId,
message: {
from: this.props.directLineClientName,
from: this.sendAsUserName,
text: messageToSend
}
}).catch((err) => console.error('Error sending message:', err));
@ -114,17 +117,17 @@ export default class BotFrameworkChat extends React.Component<IBotFrameworkChatP
watermark = response.obj.watermark;
return response.obj.messages;
})
.then((messages) => this.printMessages(messages))
.then((messages) => this.printMessages(messages));
}, this.pollInterval);
}
protected printMessages(messages) {
if (messages && messages.length) {
messages = messages.filter((m) => m.from !== this.props.directLineClientName);
messages = messages.filter((m) => m.from !== this.sendAsUserName);
if (messages.length) {
messages.forEach(this.printMessage);
}
};
}
}
protected printMessage(message) {