-
Notifications
You must be signed in to change notification settings - Fork 0
/
gs_wa_widget.js
216 lines (202 loc) · 7.56 KB
/
gs_wa_widget.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(function(window, document, undefined) {
// TODO : Common CSS object
// TODO : HTML in the Template Strings
// TODO : Append to Body
// TODO : Keep Positioning Configurable
var appId = document.currentScript.dataset.appid;
var appName = document.currentScript.dataset.appname;
var source = document.currentScript.dataset.source;
var env = document.currentScript.dataset.env;
var lang = document.currentScript.dataset.lang;
var L1, L2, L3;
function getText(lang) {
switch(lang) {
case 'de': {
L1 = 'Mit dem Absenden der folgenden Nachricht stimme ich dem Erhalt von Benachrichtigungen zu';
L2 = 'Chatten Sie mit $BusinessName auf WhatsApp.';
L3 = 'Nachricht senden';
break;
}
case 'es': {
L1 = 'Acepto recibir notificaciones enviando el siguiente mensaje';
L2 = 'Chatea con $BusinessName en WhatsApp';
L3 = 'enviar mensaje';
break;
}
case 'pt_BR': {
L1 = 'Ao enviar a seguinte mensagem, eu concordo em receber notificações.';
L2 = 'Converse com o $BusinessName no WhatsApp';
L3 = 'enviar mensagem';
break;
}
case 'ru': {
L1 = 'ОтправлÑÑ Ñледующее Ñообщение, Ñ ÑоглашаюÑÑŒ получать уведомлениÑ.';
L2 = 'Чат Ñ $BusinessName по WhatsApp';
L3 = 'Отправить Ñообщение';
break;
}
default: {
L1 = 'I agree to receive notifications by sending the following message';
L2 = 'Chat with $BusinessName on WhatsApp';
L3 = 'Send Message';
break;
}
}
}
getText(lang);
const uiWidget = `<div class="gs_wa_widgetWrapper">
<div class="gs_wa_widget">
<div class="gs_wa_widgetInner">
<div class="col w80">
<p><span style="text-align: left;">${(L2).replace('$BusinessName', appName)}</span>
</p>
<div class="check_wrapper">
<label>
<input type = "checkbox" id = "gsWaOptinCb">
<span>${L1}</span>
</label>
</div>
</div>
<div class="col w20">
<span style = "text-align: right; color:#38ae00;"><img src="https://www.buildquickbots.com/whatsapp/whatsapp_logo_stroked.png"
width = "30" height="30" style="text-align: right; color:#38ae00;">
</span>
</div>
</div>
<button class="sendMessageBtn" id="sendMessageBtn">${L3}</button>
</div>
</div>`;
const styles = `<style>
.gs_wa_widgetWrapper{
color:#777;
font-size: 12px;
box-sizing: border-box;
position:fixed;
bottom:25px;
right:25px;
z-index: 9999;
}
.gs_wa_widgetWrapper .gs_wa_widget {
position: relative;
font-family: sans-serif;
width: 320px;
height:200px;
background: #fff;
border-top-right-radius: 20px;
border-bottom-left-radius: 20px;
box-sizing: border-box;
-webkit-box-shadow: 5px 2px 5px 0px rgba(193, 193, 193, 0.75);
-moz-box-shadow: 5px 2px 5px 0px rgba(193, 193, 193, 0.75);
box-shadow: 5px 2px 5px 0px rgba(193, 193, 193, 0.75);
}
.gs_wa_widgetWrapper .check_wrapper{
color: #999;
margin: 30px auto;
}
.gs_wa_widgetWrapper .gs_wa_widgetInner{
width: 100%;
box-sizing: border-box;
}
.gs_wa_widgetWrapper .gs_wa_widgetInner p{
font-size:14px;
}
.gs_wa_widgetWrapper .col{
position: relative;
display: inline-block;
height: 155px;
padding:10px;
box-sizing: border-box;
}
.gs_wa_widgetWrapper .w80{
width: 80%;
float:left;
}
.gs_wa_widgetWrapper .w20{
text-align:center;
padding-top:17px;
width: 20%;
}
.gs_wa_widgetWrapper .sendMessageBtn{
opacity: 0.4;
background-color: #38ae00;
color: white;
position: absolute;
bottom: 0px;
appearance: none;
outline: none;
padding:15px;
width: 100%;
font-weight: 500;
border-width:0px;
border-bottom-left-radius: 20px;
cursor:pointer;
}
</style>`;
var primeElement = document.createElement('div');
primeElement.innerHTML = uiWidget;
document.body.appendChild(primeElement);
var head = document.getElementsByTagName("head")[0];
head.insertAdjacentHTML('beforeend', styles);
document.getElementById("gsWaOptinCb").addEventListener("click", () => {
onCheckClick();
}, false);
function onCheckClick(){
var checkBoxElem = document.getElementById("gsWaOptinCb");
var sendMessageBtn = document.getElementById("sendMessageBtn");
// If the checkbox is checked, then add click event listener
if (checkBoxElem.checked == true) {
sendMessageBtn.style.opacity = '1';
sendMessageBtn.addEventListener("click", function () { sendReq(); }, false);
} else {
sendMessageBtn.style.opacity = '0.4';
sendMessageBtn.removeEventListener("click", function () {}, false);
}
}
function sendReq(){
// Set up our HTTP request
var data = `a=${appId}&s=${source}`;
var xhr = new XMLHttpRequest();
// Setup our listener to process completed requests
xhr.onload = function () {
// Process our return data
if (xhr.status >= 200 && xhr.status < 300) {
// What do when the request is successful
console.log('success!', xhr);
var response = JSON.parse(xhr.response);
if(response.status && response.status == 'success'){
if(response.consent && response.consent.url){
window.open(response.consent.url, "_blank");
}
}
} else {
// What do when the request fails
console.log('The request failed!');
}
// Code that should run regardless of the request status
console.log('This always runs...');
};
var consentUrl;
switch (env) {
case 'DEV':
consentUrl = 'http://dev-smsinbox.gupshup.io/optinmanager/consent/request';
break;
case 'QA':
consentUrl = 'https://qa-smsinbox.gupshup.io/optinmanager/consent/request';
break;
case 'STAG':
consentUrl = 'https://beta.gupshup.io/optinmanager/consent/request';
break;
case 'PROD':
consentUrl = 'https://whatsapp-support.gupshup.io/optinmanager/consent/request';
break;
default:
consentUrl = 'https://whatsapp-support.gupshup.io/optinmanager/consent/request';
}
// Create and send a GET request
// The first argument is the post type (GET, POST, PUT, DELETE, etc.)
// The second argument is the endpoint URL
xhr.open("POST", consentUrl);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
}
}(window, document));