自動チャット招待のコードサンプル
このコードサンプルを使用して、Web サイトで自動チャット招待がどのように動作するかをテストおよびプレビューします。
カスタマイズされた招待を Web サイトに表示するために addButtonEventHandler() メソッドを使用する自動チャット招待のコードを次に示します。この招待では、適切なスキルを持つエージェントがチャットに対応できる場合に、顧客がエージェントとチャットを開始できます。
1<apex:page>
2
3<!-- This section creates the div with the UI for chat invitation whose id is 573D01234567890 -->
4<!-- For this usage, the "Animation" type of this invitation should be set to "Custom",
5otherwise two invitations will appear (the Salesforce-provided one and this custom one). -->
6<div id="liveagent_invite_button_573D01234567890" style="display: none; position: fixed; border: 2px solid darkblue; border-radius: 5px; background-color: lightblue; height: 100px; width: 200px;">
7 <!-- Creates an "X" option to reject or close the invitation if it's offered -->
8 <div style="cursor: pointer; padding: 5px; right: 0px; position: absolute; color: darkred; font-weight: bold;" onclick="liveagent.rejectChat('573D01234567890')">X</div>
9<!-- Provides the Start Chat option for the customer to accept or start the chat for the invitation -->
10<div style="cursor: pointer; top: 42px; left: 65px; position: absolute;font-weight: bold; font-size: 16px;" onclick="liveagent.startChat('573D01234567890')">Start Chat</div>
11</div>
12
13<!-- Live Agent Deployment Code that makes chat available -->
14<script type='text/javascript' src='https://c.la1s1.saleforceliveagent.com/content/g/js/36.0/deployment.js'></script>
15
16<script type='text/javascript'>
17 <!-- Creates the callback function used for the Live Agent chat invitation to present it or not based on availability and the customer's interaction with it -->
18 function buttonCallback(e) {
19
20 <!-- When the chat invitation is online (i.e. at least one available, skilled agent), display it at position top 200px and left 300px -->
21 if (e == liveagent.BUTTON_EVENT.BUTTON_AVAILABLE) {
22 document.getElementById('liveagent_invite_button_573D01234567890').style.display = '';
23 document.getElementById('liveagent_invite_button_573D01234567890').style.left = '300px';
24 document.getElementById('liveagent_invite_button_573D01234567890').style.top = '200px';
25 }
26
27 <!-- When the chat invitation is offline, don't display it -->
28 if (e == liveagent.BUTTON_EVENT.BUTTON_UNAVAILABLE) {
29 document.getElementById('liveagent_invite_button_573D01234567890').style.display = 'none';
30 }
31
32 <!-- When the chat invitation is accepted, stop displaying it -->
33 if (e == liveagent.BUTTON_EVENT.BUTTON_ACCEPTED) {
34 document.getElementById('liveagent_invite_button_573D01234567890').style.display = 'none';
35 }
36
37 <!-- When the chat invitation is rejected, stop displaying it -->
38 if (e == liveagent.BUTTON_EVENT.BUTTON_REJECTED) {
39 document.getElementById('liveagent_invite_button_573D01234567890').style.display = 'none';
40 }
41 }
42
43 <!-- Registers the function buttonCallback() above as the callback for the chat invitation whose id is 573D01234567890 -->
44 liveagent.addButtonEventHandler('573D01234567890', buttonCallback);
45
46 // Let's say there is data available in JavaScript that you want to use in a custom sending rule.
47 var shoppingCartValue = 123.45;
48 // To pass this data so it can be used in Sending Rules in Salesforce setup, call setCustomVariable.
49 liveagent.setCustomVariable('shoppingCartValue', shoppingCartValue);
50
51 <!-- Live Agent deployment code that initializes chat for the deployment whose id is 572D01234567890 and org is 00DD01234567890 -->
52 liveagent.init('https://d.la1s1.salesforceliveagent.com/chat', '572D01234567890', '00DD01234567890');
53
54 <!-- Enable Live Agent advanced logging to be available through the Browser's Developer Console -->
55 liveagent.enableLogging();
56</script>
57
58</apex:page>このコードによって、[設定] の招待の送信ルールで使用できるように JavaScript で使用可能なデータを渡すことができます。設定の例を次に示します。
![[設定] の自動招待の送信ルール](https://developer.salesforce.com/docs/resources/img/ja-jp/210.0?doc_id=images%2FButtonRulesSetup.png&folder=live_agent_dev)