Microsoft Bot Framework Unable to get the passed username in the bot from webchat UI - botframework

I am using the latest version of the web chat as below
``
<!DOCTYPE html>
<html>
<style>
html, body { height: 100% }
body { margin: 0 }
#webchat {
height: 100%;
width: 100%;
}
</style>
<body>
<div id="webchat" role="main"></div>
<script src="https://cdn.botframework.com/botframework-webchat/master/webchat.js"></script>
<script>
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ token: 'MY_TOKEN_FROM_AZURE' }),
userID: '1234',
username: 'Chinni',
locale: 'en-US',
botAvatarInitials: 'WC',
userAvatarInitials: 'WW'
}, document.getElementById('webchat'));
</script>
</body>
</html>
``
I sent a username from the renderwebchat() but the same is not accessible in the bot
await turnContext.SendActivityAsync(MessageFactory.Text(turnContext.Activity.From.Name), cancellationToken);
The output is null in the web chat. Can someone help me over here.
Thanks!

You may have a mistake somewhere in your implementation, because I confirm that it is working. As you can see, I have the values available:
On the bot side, here is how I send those messages:
await turnContext.SendActivityAsync(JsonConvert.SerializeObject(turnContext.Activity));
await turnContext.SendActivityAsync($"Value of 'turnContext.Activity.From.Id': '{turnContext.Activity.From.Id}', 'turnContext.Activity.From.Name': '{turnContext.Activity.From.Name}'");
And on the webchat demo page, I used the same code as yours (except that I used my own token)

Related

Can you use await in paperjs?

Very simple question, can I use functions like await inside paperjs?
I can chain together a lot of .then's and it's going fine, but as soon as I add await in front of it I get the error
SyntaxError: Unexpected token (4:22)
Please tell me they didn't make it so that I have to start wrapping everything in insanely long chains in order to integrate functions like fetch into paperjs. Below my code:
<script type="text/paperscript" canvas="myCanvas">
getCollectionList = async function() {
var collectionlist = [];
collectionlist = await fetch('/api/graph/<%= database %>');
console.log(collectionlist);
}
getCollectionList();
</script>
<canvas id="myCanvas" resize></canvas>
Your are writing your code as PaperScript which implies that Paper.js uses a JavaScript parser to parse it and apply some transformations to it.
The problem is that by default, the JavaScript parser used by Paper.js does not support ES6 features.
You have to load a more recent version of the parser (Acorn) before loading Paper.js to solve your issue.
See my answer to an analog issue here for more details.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Debug</title>
<!-- Load latest acron version first -->
<script type="text/javascript" src="https://unpkg.com/acorn"></script>
<!-- Then load Paper.js -->
<script type="text/javascript" src="https://unpkg.com/paper"></script>
<style>
html,
body {
margin: 0;
overflow: hidden;
height: 100%;
}
canvas {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<canvas id="canvas" resize></canvas>
<script type="text/paperscript" canvas="canvas">
// Here you can now use ES6 syntax.
const myAsyncFunction = async function() {
const response = await fetch('https://yesno.wtf/api/');
const json = await response.json();
console.log(json);
};
myAsyncFunction();
</script>
</body>
</html>

Web Chat: Avatar initials and image not showing

I have created a bot with MS Bot Framework and now I want to use Web Chat as a channel. Unfortunately adding avatar initials and images does not seem to work for me as it should according to the documentation.
Here is the very simple code from the documentation using botAvatarInitials to pass on the initials:
<!DOCTYPE html>
<html>
<body>
<div id="webchat" role="main"></div>
<script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script>
<script>
window.WebChat.renderWebChat(
{
directLine: window.WebChat.createDirectLine({
secret: 'YOUR_BOT_SECRET'
}),
// Passing avatar initials when rendering Web Chat
botAvatarInitials: 'BF',
userAvatarInitials: 'WC'
},
document.getElementById('webchat')
);
</script>
</body>
</html>
Any help greatly appreciated
It looks like this is documented incorrectly. The botAvatarInitials and the userAvatarInitials need to be set in the style options.
const styleOptions = {
botAvatarInitials: 'BF',
userAvatarInitials: 'WC'
};
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({secret: 'YOUR_BOT_SECRET'}),
styleOptions
},
document.getElementById('webchat')
);
For more details, take a look at the Display User and Bot Initials Web Chat sample.

Deploy customize webchat bot framework

I clone this repo https://github.com/Microsoft/BotFramework-WebChat and I managed to edit css in local.
I would like to know what I need to deploy to use webchat online ?
Thanks you for you help
I clone this repo https://github.com/Microsoft/BotFramework-WebChat and I managed to edit css in local. I would like to know what I need to deploy to use webchat online?
After you customize&build your own web chat, to embed web chat in your website, you should include your built botchat.css and botchat.js file in your project and reference botchat.css and botchat.js in your web page.
I want to add a widget like this to open my chatbot, what I need to do?
It seems that you’d like to display the chat icon in your web page to allow user dynamically open/collapse your web chat, to achieve the requirement, you need not to modify the botchat.js file, the following code sample work for me , you can refer to it.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link href="BotChat/botchat.css" rel="stylesheet" />
<script src="BotChat/botchat.js"></script>
<style>
#mychat {
margin: 10px;
position: fixed;
bottom: 30px;
right: 10px;
z-index: 1000000;
}
</style>
</head>
<body>
<div id="container">
<h1>Hello World</h1>
<!--other page contents-->
<img id="mychat" src="https://i.stack.imgur.com/RD7i4.png" style="float:right" />
</div>
</body>
</html>
<script>
(function () {
var div = document.createElement("div");
document.getElementsByTagName('body')[0].appendChild(div);
div.outerHTML = "<div id='botDiv' style='width: 400px; height: 0px; margin:10px; position: fixed; bottom: 0; right:0; z-index: 1000;><div id='botTitleBar' style='height: 40px; width: 400px; position:fixed; cursor: pointer;'></div></div>";
BotChat.App({
directLine: { secret: '{directline_secret}' },
user: { id: 'You' },
bot: { id: '{your_botid}' }
}, document.getElementById("botDiv"));
document.getElementsByClassName("wc-header")[0].setAttribute("id", "chatbotheader");
document.querySelector('body').addEventListener('click', function (e) {
e.target.matches = e.target.matches || e.target.msMatchesSelector;
if (e.target.matches('#chatbotheader')) {
var botDiv = document.querySelector('#botDiv');
botDiv.style.height = "0px";
document.getElementById("mychat").style.display = "block";
};
});
document.getElementById("mychat").addEventListener("click", function (e) {
document.getElementById("botDiv").style.height = '500px';
e.target.style.display = "none";
})
}());
</script>
Test result:
If I want to add a widget like this
to open my chatbot, what I need to do ? Is it enough to modify the botchat.js file ?
How to have an application like in this post?
if you want to use iframe then you can't customize the design . for customize design you need to use Direct line by you can change design as well as make bot fully responsive.
Here is the code i am using working fine for me:
<!DOCTYPE html>
<html class="no-js lang-en" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<!-- For Bot Frame -->
<link href="../assets/SCSS/botchat-fullwindow.css" type="text/css" rel="stylesheet" />
<link href="../assets/SCSS/botchat.css" type="text/css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body class="personal-body">
<!-- For Bot Code -->
<div id="BotChatElement">
</div>
<img src="../assets/images/chat-2-icon.png" onclick="openchatbot()" class="open-bot" id="button_chat" />
<script src="../assets/JS/botchat.js"></script>
<script>
var model = {
"userId": "demo1234",
"userName": "You",
"botId": "Chatbot Main Tree",
"botIconUrl": "",
"botName": "Chatbot Main Tree",
"secret": "",
"iconUrl": "",
"directLineUrl": "https://webchat.botframework.com/v3/directline",
"webSocketEnabled": "false"
};[enter image description here][1]
BotChat.App({
directLine: {
secret: model.secret,
token: model.token,
domain: model.directLineUrl,
webSocket: false,
sendTyping: true
},
user: { id: model.userId, name: model.userName },
bot: { id: model.botId, name: model.botName },
sendTyping: true,
resize: 'window'
},
document.getElementById("BotChatElement"));
function openchatbot() {
$(".wc-chatview-panel *").removeClass("maximizebot");
$(".wc-chatview-panel").css({ 'bottom': '0', 'display': 'block' });
}
$(".minimize-icon").on('click',
function () {
$(".wc-chatview-panel").removeClass("minimize-icon");
$(".wc-chatview-panel ").toggleClass("minimizebot");
$(".minimize-icon ").toggleClass("maximizebot");
});
$(".close-icon").on('click',
function () {
$(".wc-chatview-panel ").removeClass("minimizebot");
$(".wc-chatview-panel ").removeClass("maximizebot");
$(".wc-chatview-panel").css({ 'bottom': '-500px', 'display': 'none' });
});
</script>
</body>
</html>
To deploy your bot follow those steps:
Create an Azure account.
You can create free trial Azure subscription from here.
Deploying the a MS Bot framework code to Azure:
You can see this video deploying a MS Bot framework code to Azure
WebChat channel using Visual Studio.
Also you can check this tutorial. (However, this tutorial use old version of Azure portal).
After connecting the bot with WebChat channel, you can use the Embed code in your HTML code.

How can i make chat bot appear as live chat using direct line API?

<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
</head>
<body>
<div id="bot"/>
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<script>
BotChat.App({
directLine: { secret: direct_line_secret },
user: { id: 'userid' },
bot: { id: 'botid' },
resize: 'detect'
}, document.getElementById("bot"));
</script>
</body>
</html>
I have this code to embed a bot as a live chat using direct Line API instead of the usual iframe but when i put in my directline secrete key, the Bot is occupying the whole web page. I need it to appear by the bottom right of the web page and pop up as a life chat when a user clicks on it. Please someone should guide me in achieving this. Thanks
Your problem is about the way you are displaying your div including the bot: <div id="bot"/>
You can style this div to appear like you want; there are several samples provided on the bot Webchat Github project:
For sidebar display: https://github.com/Microsoft/BotFramework-WebChat/blob/master/samples/sidebar/index.html
For fullwindow : https://github.com/Microsoft/BotFramework-WebChat/tree/master/samples/fullwindow
I would highly suggest to take a look at the 1st sample which has a demo of narrow, normal and wide div
As Nicolas R suggested, you can style the container div <div id="bot"/> to position it at bottom right corner of web page. I achieved same requirement in a project, the following sample code works for me, you can refer to it.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<style>
.wc-chatview-panel {
width: 350px;
height: 450px;
position: relative;
}
#bot {
position: fixed;
bottom: 0;
right: 0;
}
.closechat {
top: 405px !important;
}
</style>
</head>
<body>
<div id="bot"></div>
</body>
</html>
<script>
BotChat.App({
directLine: { secret: "{your_directline_secret}" },
user: { id: 'You' },
bot: { id: '{Your_BotId}' },
resize: 'detect'
}, document.getElementById("bot"));
$(function () {
$("div.wc-header").prop("isopen", "true");
$("div.wc-header").click(function () {
var isopen = $(this).prop("isopen");
//alert(isopen);
if (isopen == "true") {
$("div.wc-chatview-panel").addClass("closechat");
$("div.wc-header").prop("isopen", "false");
} else {
$("div.wc-chatview-panel.closechat").removeClass("closechat");
$("div.wc-header").prop("isopen", "true");
}
})
})
</script>
Test result:

Kendo Vue datasource for Kendo grid with Auth Header

I am trying to create a datasource for a Kendo Grid in Vue.js. I need to put auth headers so declarative syntax in the template does not fix my problem.
The link below makes it possible for an array, but I need an example with an AJAX call or a promise (an Axios one would be perfect).
Kendo UI Grid Data variable Vue.js
A much cleaner answer came from Telerik support today. It make the world a better place :)
<!DOCTYPE html>
<html>
<head>
<base href="http://demos.telerik.com/kendo-vue-ui/wrappers/dropdownlist/index">
<style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
<title></title>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.common-material.min.css" />
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.material.min.css" />
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1018/styles/kendo.material.mobile.min.css" />
<script src="https://kendo.cdn.telerik.com/2017.3.1018/js/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2017.3.1018/js/kendo.all.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.15/browser-polyfill.min.js"></script>
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>
<script src="https://unpkg.com/#progress/kendo-all-vue-wrapper/dist/cdn/kendo-all-vue-wrapper.min.js"></script>
</head>
<body>
<div id="example">
<div id="app" class="demo-section k-content">
<h4>Find a product</h4>
<kendo-datasource ref="datasource"
:type="'odata'"
:server-filtering="true"
:transport-read="{ url: 'https://demos.telerik.com/kendo-ui/service/Northwind.svc/Products', beforeSend: onBeforeSend }">
</kendo-datasource>
<kendo-dropdownlist ref="dropdownlist"
:data-text-field="'ProductName'"
:filter="'contains'"
:auto-bind="true"
:data-source-ref="'datasource'">
</kendo-dropdownlist>
</div>
</div>
<style>
.demo-section .k-dropdown {
width: 100%;
}
</style>
<script>
new Vue({
el: '#app',
methods: {
onBeforeSend: function(xhr) {
var token = "asd81237hdbsjkfh234uygr38fg73";
xhr.setRequestHeader('Authorization', 'Bearer ' + token)
}
}
})
</script>
</body>
</html>
Ok, so the documentation is a bit flaky, but I managed to get a custom function going in Vue, just like in the plain Kendo UI datasource. Look at this demo for reference: http://dojo.telerik.com/uXELIh
This is a mix of declarative and custom methods, so it might look a bit odd. (I've never worked with the VUE wrapper before)
Instead of the transport-read-url="uri/to/somewhere" property just define a transport-read="readData" property.
<kendo-datasource ref="datasource1"
:transport-read="readData"
:schema-model-id="'ProductID'"
:schema-model-fields="schemaModelFields"
:batch='true'
:page-size='20'>
</kendo-datasource>
Then create the readData method:
new Vue({
el: '#app',
data: {
schemaModelFields: {
/*...*/
}
},
methods:
readData: function(e) {
//this simply returns one Product with a name Chai as a dummy
//set your Auth headers here, do the request and then pass
//the data in the e.success method as a parameter
e.success({ProductName: "Chai"})
}
}
/*...*/
});
That's all there is.
However If you have an Auth header, that you need to prepend to all your ajax requests, I'd suggest you use $.ajaxSetup() (How can I add a custom HTTP header to ajax request with js or jQuery?). This will save you the hassle of implementing this for read, update, create and delete, each and every time.
$.ajaxSetup({
headers: { 'x-my-custom-header': 'some value' }
});
A bit painful but I was able to do it with Marco's help
Here is the code
<template>
<div>
<kendo-datasource ref="datasource1"
:transport-read="readData"
:batch='true'
:page-size='20'>
</kendo-datasource>
<kendo-grid :height="550"
:data-source-ref="'datasource1'"
:pageable='true'>
</kendo-grid>
</div>
</template>
<script>
export default {
name: 'grid',
computed: {
token () {
return this.$store.state.access_token
}
},
methods: {
readData: function (e) {
console.log(this.token)
var tkn= this.token
$.ajax({
url: 'http://127.0.0.1/AssetExtranet.WebAPI2/api/Vw_Hmn_Branch',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + tkn)
},
dataType: 'json',
success: function (data) {
e.success(data)
},
type: 'GET'
})
}
}
}
</script>
Thank Marco and Vue is perfect and super fast. It is a joy to work with Vue after that Angular 2,3,4,5... mess.

Resources