The Context Service is part of the Live Assist for Microsoft Dynamics 365 SDK (Live Assist SDK). The Context Service allows bots to build contextual information about their engagements with visitors, so that it can be made available to Agent within the CRM when they grab a chat. The Bot must collect the Context Data before sending it onto the Context Service. The Context Data collects asserted visitor details and information about the visitor's interaction on your website.
Step 1 — Extend the C# Bot as an Example Quick Start
This article follows on from the Bot created in the first article on our series:
C# Bot as an Enabler—Azure Function Bot (Quick Start)
This article will extend the code example found in the first article to:
- Make Echo Bot build some context data for a visitor,
- Encrypt the data and pass it to the Live Assist Context Service
- Enable the Live Assist Context Service to collect the context data.
Step 2 — Code Changes to Echobot as an Enabler
There is an extra dependency added to the project.json file:
"jose-jwt": "2.4.0"
The code changes for Function Bots are identical to the code changes in Step 2 of the Web App Bot Context Service. Rather than unnecessarily duplicate the descriptions of the node the extended code snippets for this example are in our Git Repository
Step 3 — Generate and managing a public/private key pair for the Context Service
Just like in our Web App Bot article, this bot will need have access to a Certificate Key Store, loaded into Azure.
This is the recommended approach for loading certificate stores in code, as it does not rely on the developer having access to the private key. The certificate store can be retrieved from the Azure bot framework using a fingerprint. The process is outlined in the Microsoft Azure App Service Documentation . Function bots require the same configuration for managing Certificate Stores.
Each bot must create it's own public/private key pair so that it can encrypt the Context Data sent to the Live Assist Context Service. Your Live Assist organization must be provided the public-key to decypt the data.
You can then see the private certificate and corresponding thumbprint, specified in code:
This should be the same as the fingerprint produced in Step 3 and specified in the Jwt.csx code.
There are numerous ways to generate private keys, so the following is only provided as an example. Please be familiar with the common certificate and certificate-store file extensions such as (pem, crt, cer and .pfx). This example uses a self-signed certificate pair generated with OpenSSL. Developing bots may require different RSA keys or provide certificates signed by your organization's CA.
// Create the Private Key specify a CN. openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt Common Name (eg, your name or your server's hostname) []:cafex.example.com //Create a Certificate Store and assign it a password openssl pkcs12 -export -in certificate.crt -inkey privateKey.key -out certificate_store.pfx Enter Export Password: XXXX // Retrieve the Public Key for decryption openssl x509 -inform pem -in certificate.crt -pubkey -out certificate_publickey.pem -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5DJvO/+PZ3H/DP2E09Vp 6ocS7+p0d6ERR/TZNZoT3e3kYnH79bsue/HNz+IJRCk2zCFNVszkF9+iB/JDDQE2 7/gvl0tah8vnfFirr5g3zs1lCkIhEgnJ1reUeRMG/azyn4ZRY+6Fug/AvQqihMLO VI5fw8pql2ZjSwQHWS+aEuFXqlgOQ8VNjIxVx3Zk1BvABfgQbTmt5hLa9YSyhXQp Ln/0XHTnNMEQEsWbA6iK3UdS2/M7/ejNeA2pAIk2HJE4GDpsBo2yBaBQE7vzdJKw smyrLYXm7ws25opElnNbYgy99KXDtYMTP6/cOyouXSgvpWlSeSZrAG597kQyJQE+ nwIDAQAB -----END PUBLIC KEY----- //Fingerprint/Thumbprint needed for certificate identification openssl x509 -in certificate.crt -fingerprint -noout SHA1 Fingerprint=AA:78:DF:90:3B:DF:B7:34:BE:A8:9E:E4:C5:35:F4:BB:E8:5A:1A:5C
Step 4 — Loading the Public Key into the Live Assist Admin Portal
Your Live Assist organization needs access to the Public Key to access the Context Store. Keys can be added via the Live Assist Admin Portal.
Please read How to Log into the Admin Portal if necessary.
From the Admin Portal select Link > Context Keys
On the following page you can provide a name for the Public Key and then paste in the key you collected in the previous step.
The Public Key name can be anything, but we would recommend using the Bot's Name and/or the certificate Fingerprint to identify it.
Step 6 — Testing Web Chat Bot
The Bot can be tested from the Azure Bot Services, which is exactly how the Web Chat was tested in the previous Function Bot.
The Agent will now see the Context Data the Bot Set (in Step 2):
Step 7 — Debugging the bot
Details on debugging the bot can be found in the previous article.