forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add programmatic post activity sample
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Sample - Programmatic access to post activity | ||
|
||
When a button is clicked, it will programmatically send a message to the bot. | ||
|
||
# Test out the hosted sample | ||
|
||
- [Try out MockBot](https://microsoft.github.io/BotFramework-WebChat/15.c.programmatic-post-activity) | ||
|
||
# Things to try out | ||
|
||
- Click the "Help" button on uperr-left hand corner | ||
|
||
# Further reading | ||
|
||
## Related samples | ||
|
||
[`15.b.incoming-activity-event`](https://github.com/Microsoft/BotFramework-WebChat/tree/master/samples/15.a.incoming-activity-event) is a sample which will fire JavaScript event on all incoming activity. | ||
|
||
## Full list of Web Chat hosted samples | ||
|
||
View the list of available samples by clicking [here](https://github.com/Microsoft/BotFramework-WebChat/tree/master/samples) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<!DOCTYPE html> | ||
<html lang="en-US"> | ||
<head> | ||
<title>Web Chat: Programmatic access to post activity</title> | ||
<!-- | ||
For demonstration purposes, we are using the development branch of Web Chat at "/master/webchat.js". | ||
When you are using Web Chat for production, you should use the latest stable release at "/latest/webchat.js", | ||
or lock down on a specific version with the following format: "/4.1.0/webchat.js". | ||
--> | ||
<script src="https://cdn.botframework.com/botframework-webchat/master/webchat.js"></script> | ||
<style> | ||
html, body { height: 100% } | ||
body { margin: 0 } | ||
|
||
#webchat, | ||
#webchat > * { | ||
height: 100%; | ||
width: 100%; | ||
} | ||
|
||
#helpButton { | ||
left: 10px; | ||
position: absolute; | ||
top: 10px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="webchat" role="main"></div> | ||
<button id="helpButton" type="button">Help</button> | ||
<script> | ||
(async function () { | ||
// In this demo, we are using Direct Line token from MockBot. | ||
// To talk to your bot, you should use the token exchanged using your Direct Line secret. | ||
// You should never put the Direct Line secret in the browser or client app. | ||
// https://docs.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-direct-line-3-0-authentication | ||
|
||
const res = await fetch('https://webchat-mockbot.azurewebsites.net/directline/token', { method: 'POST' }); | ||
const { token } = await res.json(); | ||
|
||
// We are creating the Web Chat store here so we can dispatch WEB_CHAT/SEND_MESSAGE action later. | ||
const store = window.WebChat.createStore(); | ||
|
||
window.WebChat.renderWebChat({ | ||
directLine: window.WebChat.createDirectLine({ token }), | ||
// We are passing our own version of Web Chat store. | ||
store | ||
}, document.getElementById('webchat')); | ||
|
||
// Hook into the button click, when clicked, will send "help" message to the bot. | ||
document.querySelector('#helpButton').addEventListener('click', () => { | ||
store.dispatch({ | ||
type: 'WEB_CHAT/SEND_MESSAGE', | ||
payload: { text: 'help' } | ||
}); | ||
}); | ||
|
||
document.querySelector('#webchat > *').focus(); | ||
})().catch(err => console.error(err)); | ||
</script> | ||
</body> | ||
</html> |