Skip to content

Instantly share code, notes, and snippets.

@sohalloran
Created December 6, 2018 11:22
Show Gist options
  • Select an option

  • Save sohalloran/990ca79501cedefaba7b1e5153856569 to your computer and use it in GitHub Desktop.

Select an option

Save sohalloran/990ca79501cedefaba7b1e5153856569 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Create a live agent session, polling every second for messages
#
domain="https://[DOMAIN-HERE]salesforceliveagent.com"
orgid="ORG-ID-HERE"
deploymentid="DEPLOYMENT-ID-HERE"
buttonid="BUTTON-ID-HERE"
createchat ()
{
# Create a new session
json=$(curl -H "X-LIVEAGENT-API-VERSION: 43" -H "X-LIVEAGENT-AFFINITY:null" $domain/chat/rest/System/SessionId)
echo $json | jq
skey=$(echo $json | jq -r .key)
sid=$(echo $json | jq -r .id)
token=$(echo $json | jq -r .affinityToken)
# Request a new chat
curl -0 -X POST $domain/chat/rest/Chasitor/ChasitorInit \
-H "X-LIVEAGENT-API-VERSION: 43" -H "X-LIVEAGENT-SESSION-KEY: $skey" \
-H "X-LIVEAGENT-AFFINITY: $token" -H "Content-Type: application/json" \
-d "{'sessionId':'$sid','organizationId':'$orgid','deploymentId':'$deploymentid','buttonId':'$buttonid','userAgent': 'Mozilla/5.0','language': 'en-US','screenResolution':'2560x1440','prechatDetails': [],'prechatEntities': [],'visitorName': $1,'receiveQueueUpdates': 'true','isPost': 'true'}"
# Poll every second updates
for i in {1..6}
do
init=$(curl -H "X-LIVEAGENT-API-VERSION: 43" -H "X-LIVEAGENT-AFFINITY:$token" -H "X-LIVEAGENT-SESSION-KEY:$skey" $domain/chat/rest/System/Messages)
echo '-------------'
echo $1
echo $init | jq -r '.messages[] | {queuePosition: .message.queuePosition, updatedPosition: .message.position}'
echo '-------------'
sleep 1
done
# End chat
curl -0 -X POST $domain/chat/rest/Chasitor/ChatEnd \
-H "X-LIVEAGENT-API-VERSION: 43" -H "X-LIVEAGENT-SESSION-KEY: $skey" \
-H "X-LIVEAGENT-AFFINITY: $token" -H "Content-Type: application/json" \
-d "{reason: 'client'}"
}
# Create 5 sessions
for i in {1..5}
do
createchat "chat-$i" &
done
#sleep 20
# Create 5 more sessions
for i in {6..10}
do
createchat "chat-$i" &
done
#sleep 20
# Create 5 more sessions
for i in {11..15}
do
createchat "chat-$i" &
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment