-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebchat.js
79 lines (70 loc) · 2.53 KB
/
webchat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*-----------------------------------------------------------------------------
__ __
_______/ |______ ____ | | __
/ ___/\ __\__ \ _/ ___\| |/ /
\___ \ | | / __ \\ \___| <
/____ > |__| (____ /\___ >__|_ \
\/ \/ \/ \/
.__
______ _____ _____ _____| |__ ___________ ______
/ ___// \\__ \ / ___/ | \_/ __ \_ __ \/ ___/
\___ \| Y Y \/ __ \_\___ \| Y \ ___/| | \/\___ \
/____ >__|_| (____ /____ >___| /\___ >__| /____ >
\/ \/ \/ \/ \/ \/ \/
@authors: Andrea Simeoni <andreasimeoni84@gmail.com>
-----------------------------------------------------------------------------*/
var MyWebChat = function(params) {
// Webchat client args
var user = {
id: params['userid'] || 'userid',
name: params["username"] || 'username'
};
var bot = {
id: params['botid'] || 'botid',
name: params["botname"] || 'botname'
};
// Create Directline connection and application
var botConnection = new BotChat.DirectLine({
secret: params['s'],
webSocket: params['webSocket'] && params['webSocket'] === "true" // defaults to true
});
BotChat.App({
botConnection: botConnection,
user: user,
bot: bot
}, document.getElementById(params['targetElement']));
this.loadApplication = function() {
/**
* Sends custom parameter to the chatbot
**/
var sendCustomArg = function() {
console.log('post message');
botConnection
.postActivity({
type: "event",
value: params['customArg'],
from: {
id: params['userid']
},
name: "customArgName"
})
.subscribe(id => console.log("YOUR CUSTOM ARG HAS BEEN SENT"));
}
sendCustomArg();
/**
* When window unloads send endOfConversation
* This event is catched by the bot that can freeup server resources
**/
window.onbeforeunload = function() {
botConnection
.postActivity({
type: "endOfConversation",
from: {
id: params['userid']
}
})
.subscribe(id => console.log("endOfConversation ack"));
};
};
return this;
};