27
27
border-left : 4px solid # 6c757d ;
28
28
margin-bottom : 20px ;
29
29
}
30
+ .control-buttons {
31
+ margin : 20px 0 ;
32
+ }
33
+ .control-buttons button {
34
+ margin-right : 10px ;
35
+ padding : 8px 16px ;
36
+ background-color : # 1a5b6e ;
37
+ color : white;
38
+ border : none;
39
+ border-radius : 4px ;
40
+ cursor : pointer;
41
+ }
42
+ .control-buttons button : hover {
43
+ background-color : # 107180 ;
44
+ }
45
+ # programmatic-embedded-bot {
46
+ height : 500px ;
47
+ margin-top : 20px ;
48
+ border : 1px solid # ddd ;
49
+ border-radius : 5px ;
50
+ }
30
51
</ style >
31
52
</ head >
32
53
< body class ="user-logged-in ">
@@ -75,6 +96,18 @@ <h2>Method 3: Explicitly Call JavaScript Function</h2>
75
96
< div id ="custom-qa-bot "> </ div >
76
97
</ div >
77
98
99
+ < div class ="demo-section ">
100
+ < h2 > Method 4: Programmatically Control Embedded Bot</ h2 >
101
+ < p > Control the embedded bot programmatically using the exposed ref methods:</ p >
102
+ < div class ="control-buttons ">
103
+ < button onclick ="openBot() "> Open Bot</ button >
104
+ < button onclick ="closeBot() "> Close Bot</ button >
105
+ < button onclick ="toggleBot() "> Toggle Bot</ button >
106
+ < button onclick ="checkBotState() "> Check State</ button >
107
+ </ div >
108
+ < div id ="programmatic-embedded-bot "> </ div >
109
+ </ div >
110
+
78
111
< script >
79
112
// Determine if user is logged in by checking for a class on the body
80
113
function isAnonymous ( ) {
@@ -86,11 +119,13 @@ <h2>Method 3: Explicitly Call JavaScript Function</h2>
86
119
function initializeQABot ( ) {
87
120
// Method 1: Auto-detect div#qa-bot
88
121
const qaBot = document . getElementById ( 'qa-bot' ) ;
122
+ console . log ( "| initializeQABot qaBot:" , qaBot ) ;
89
123
if ( qaBot && window . qAndATool && ! qaBot . hasChildNodes ( ) ) {
90
124
window . qAndATool ( {
91
125
target : qaBot ,
92
126
isLoggedIn : ! window . isAnonymous ,
93
- isAnonymous : window . isAnonymous
127
+ isAnonymous : window . isAnonymous ,
128
+ defaultOpen : true
94
129
} ) ;
95
130
}
96
131
@@ -104,11 +139,41 @@ <h2>Method 3: Explicitly Call JavaScript Function</h2>
104
139
prompt : container . dataset . prompt || "Ask me a question..." ,
105
140
isLoggedIn : ! window . isAnonymous ,
106
141
isAnonymous : window . isAnonymous ,
107
- isOpen : false
142
+ defaultOpen : false
108
143
} ) ;
109
144
}
110
145
} ) ;
111
146
}
147
+
148
+ // Store reference to programmatic bot
149
+ let programmaticBotRef = null ;
150
+
151
+ // Functions to control the bot programmatically
152
+ function openBot ( ) {
153
+ if ( programmaticBotRef && programmaticBotRef . open ) {
154
+ programmaticBotRef . open ( ) ;
155
+ }
156
+ }
157
+
158
+ function closeBot ( ) {
159
+ if ( programmaticBotRef && programmaticBotRef . close ) {
160
+ programmaticBotRef . close ( ) ;
161
+ }
162
+ }
163
+
164
+ function toggleBot ( ) {
165
+ if ( programmaticBotRef && programmaticBotRef . toggle ) {
166
+ const isOpen = programmaticBotRef . toggle ( ) ;
167
+ console . log ( 'Bot is now' , isOpen ? 'open' : 'closed' ) ;
168
+ }
169
+ }
170
+
171
+ function checkBotState ( ) {
172
+ if ( programmaticBotRef && programmaticBotRef . isOpen ) {
173
+ const isOpen = programmaticBotRef . isOpen ( ) ;
174
+ alert ( `Bot is currently ${ isOpen ? 'open' : 'closed' } ` ) ;
175
+ }
176
+ }
112
177
</ script >
113
178
114
179
<!--
@@ -138,11 +203,26 @@ <h2>Method 3: Explicitly Call JavaScript Function</h2>
138
203
prompt : "Try asking a question about ACCESS..." ,
139
204
isLoggedIn : ! window . isAnonymous ,
140
205
isAnonymous : window . isAnonymous ,
141
- isOpen : false
206
+ defaultOpen : false
142
207
} ) ;
143
208
} else if ( ! window . qAndATool ) {
144
209
console . error ( "qAndATool function not found. Make sure the JS files are loaded properly." ) ;
145
210
}
211
+
212
+ // Method 4 (programmatic control)
213
+ const programmaticBot = document . getElementById ( 'programmatic-embedded-bot' ) ;
214
+ if ( window . qAndATool && programmaticBot && ! programmaticBot . hasChildNodes ( ) ) {
215
+ programmaticBotRef = window . qAndATool ( {
216
+ target : programmaticBot ,
217
+ embedded : true ,
218
+ welcome : "I can be controlled programmatically!" ,
219
+ prompt : "Ask me a question about ACCESS..." ,
220
+ isLoggedIn : ! window . isAnonymous ,
221
+ isAnonymous : window . isAnonymous ,
222
+ defaultOpen : false ,
223
+ returnRef : true // Important: this makes qAndATool return the ref
224
+ } ) ;
225
+ }
146
226
} ) ;
147
227
</ script >
148
228
</ body >
0 commit comments