Microsoft has released a set of intelligence APIs known as Cognitive Services which cover a wide range of categories such as vision, speech, language, knowledge and search. The APIs can analyze images, detect emotions, spell check and even translate text, recommend products and more. In this post I will cover how the Text Analysis API can be used to determine the sentiment of your client based on the emails they send.
The idea is that any time a client (Contact in this case) sends an email that is tracked in CRM, then we will pass it to the Text Analysis API to see if the sentiment is positive or negative. In order to do this, we will want to register a plugin on create of email. We will make the plugin asynchronous since we’re using a third party API and do not want to slow down the performance of the email creation if the API is executing slowly. We will also make the plugin generic and utilize the secure or unsecure configuration when registering the plugin to pass in the API Key as well as the schema name of the sentiment field that will be used.
Below is the constructor of the plugin to take in either a secure or unsecure configuration expecting the format of “<API_KEY>|<SENTIMENT_FIELD>”.
Next is the Execute method of the Plugin which will retrieve the email description from the email record and pass it to the AnalyzeText method. The AnalyzeText method will return the sentiment value which we will then use the populate the sentiment field on the email record.
Then we have the AnalyzeText method which will pass the email body to the Text Analysis API which then returns the sentiment value.
And finally the classes used as input and output parameters for the Text Analysis API.
Now register the plugin on post-Create of Email with the Text Analysis API Key and the schema name of the sentiment field either in the secure or unsecure configuration
Now when an email is created in CRM, once the asynchronous job is complete, the email record will have a Sentiment value set from a range of 0 (negative) to 1 (positive).
The sentiment field on the email record can then be used as a calculated field on the contact to average the sentiment values of all the email records where the contact is the sender to track the sentiment of your clients based on the emails they send you.