How to save human service history via Blip Integration with Google Drive May 16, 2023 15:44 Updated Index: Project creation Enabling the Google Drive API OAuth permission screen Credential creation Authorize Credentials Flow configuration See how to save human service history using Blip's integration with the Google Drive API and save the transcript of your team's conversations. —------ Through the Blip integration with Google Drive, it is possible that, at the end of each human service, a .txt file is generated in Google Drive with the transcription of the conversation, maintaining a backup that allows you to save your chatbot's human service history. Project creation Access Google Developers and on your home screen, create a project. Note: If the project is not already selected, just access the menu like the gif above and select it. Enabling the Google Drive API In this step, we will access the menu option on the left side, APIs and Services → Library, search for Google Drive and finally activate it. Note: if the API is not activated on the first try, just click the Activate button again. OAuth permission screen With the API enabled, let's configure external permissioning. To do this, in the left menu select the OAuth permission screen option, now you will see two options for User Type, just select External and click CREATE, as shown in the example below: Now, to finish this step, you must define a name for the API. Enter the email for support and the developers (remember, you must not fill in any other fields). Scroll to the bottom of the page and click SAVE AND CONTINUE. Then, on this next page, click the button at the bottom of the page to SAVE AND CONTINUE. You will reach the end and you must click on the button Back to the Panel and finally Publish Application. Credential creation Now let's create the Drive access credentials that will be used by Blip. To do so, in the left menu click Credentials → Create Credentials → OAuth Client ID. In the following screen, choose the application type as Web Application, new options will be listed, scroll to the bottom of the page and click Add URI in the Authorized Redirect URIs option and include OAuth 2.0 Playground and click Create. A pop-up will appear with the client ID and secret key, copy the values, SAVE and click Ok. Authorize credentials To finalize the settings related to Google Drive, let's access OAuth 2.0 Playground. After that, click on the gear in the upper right corner. In the pop-up, enable the Use your own OAuth credentials option and fill in the data you collected in the previous step, as shown in the example below: Now just press CRTL+F and look for the word Drive. Select the option to expand it, and then select the option that has https://www.googleapis.com/auth/drive 1, like the example below: A new window will open. In it, select the email you will use to authorize and click Allow, as in the example below: Finally, click on Exchange authorization code for tokens to obtain the refresh token, copy its value and save with the credentials already saved in the previous steps. Flow configuration The first step to save the human service history is to get the request token from the flow in the Blip Builder. For this, in the entry actions of the block immediately following the human service, the following settings must be made. Create an input action of type HTTP Request. Method will be POST, URL will be https://accounts.google.com/o/oauth2/token with empty Headers. Ultimately, the Body will be: { "client_secret": "seu_id_secreto", "grant_type": "refresh_token", "refresh_token": "seu_refresh_token", "client_id": "seu_id_de_cliente"} Save the response output variables in statusAuthGoogle and the response body in oauth, as shown in the image below. Note: Do not forget to change the client_secret, refresh_token and client_id fields to those obtained in the previous steps, the grant_type must remain with refresh_token value. Just below we will use the Blip API to return the transcription of the conversation, configure a new HTTP request, the Method will be POST , the URL will be https://{{contractid}}.http.msging.net/commands with Content-Type Headers with application/ value json and Authorization with the human service bot key.Note: Use the url with the contract id to consume the endpoints reported below, its performance and operation may be impacted if it does not have the contract id, so it is essential to use the url with the contract id to use http requests!Ultimately, the Body will be: { "id": "{{random.guid}}", "to": "postmaster@desk.msging.net", "method": "get", "uri": "/tickets/{{input.content@id}}/messages?attendanceTranscriptionOnly=true"} Save the response output variables in statusHistory and the response body in responseHistory, as shown in the image below. Below this request, create an action of type Execute script passing responseHistory as an input parameter and saving the return in history. The body of the script should be: function run(input) {var messages = '';try{// remove non-printable and other non-valid JSON charsinput = input.replace(/\\n/g, " ").replace(/\\'/g, "\\'").replace(/\\"/g, '\\"').replace(/\\&/g, "\\&").replace(/\\r/g, " ").replace(/\\t/g, " ").replace(/\\b/g, "\\b").replace(/\\f/g, "\\f");input = input.replace(/[\u0000-\u0019]+/g,"");obj = JSON.parse(input);i = 0;while(obj["resource"]["items"][i] != null && i < obj["resource"]["items"].length){var o = obj["resource"]["items"][i];if(o != null){if(o["type"] == 'text/plain'){if(o["direction"] == 'received'){messages = messages + '\n['+dateFormat(o["date"])+'][Usuario] ' + o["content"].replace("<b>","").replace("</b>","");}else{messages = messages + '\n['+dateFormat(o["date"])+'][Atendente] ' + o["content"].replace("<b>","").replace("</b>","");}}else if(o["type"] == 'application/vnd.lime.select+json'){t = '\n['+dateFormat(o["date"])+'][Atendente] ' + o["content"]["text"];for(var j=0; j < o["content"]["options"].length; j++){t = t + " - " + o["content"]["options"][j]["text"].replace("<b>","").replace("</b>","");}messages = messages + t;}else if(o["type"] == 'application/vnd.lime.media-link+json'){if(o["direction"] == 'received'){messages = messages + '\n['+dateFormat(o["date"])+'][Usuario] <Mídia>';}else{messages = messages + '\n['+dateFormat(o["date"])+'][Atendente] <Mídia>';}}else if(o["type"] != 'application/vnd.iris.ticket+json'){if(o["direction"] == 'received'){messages = messages + '\n['+dateFormat(o["date"])+'][Usuario] Conteúdo indisponível';}else{messages = messages + '\n['+dateFormat(o["date"])+'][Atendente] Conteúdo indisponível';}}}i = i+1;}}catch(e){}return messages.trim();}function dateFormat(input){let d = new Date(input);let dStr = ("0" + d.getDate()).slice(-2) + "-" + ("0" + (d.getMonth()+1)).slice(-2)+"-"+d.getFullYear();dStr = dStr + " "+("0" + d.getHours()).slice(-2)+":"+("0" + d.getMinutes()).slice(-2);return dStr;} As shown in the image below: Upload the file to Google Drive Now let's upload the file to Google Drive. Create another HTTP request, with Method will be POST , URL will be https://www.googleapis.com/upload/drive/v3/files?uploadType=media with Headers Content-Type with value application/json and Authorization with value Bearer {{oauth@access_token}} . Finally, the Body will be just {{history}}, as shown in the image below. Save the response output variables in statusSend and the response body in responseSend. Ready! The service history that went through this flow will be sent to the Google Drive account linked to the authorized email. However, it will be in JSON format and without a defined name. Now create one more HTTP request, with Method will be PATCH , URL will be https://www.googleapis.com/drive/v3/files/{{responseSend@id}} with Authorization Header with value Bearer {{oauth@ access_token}}. Ultimately, the Body will be: {"name":"{{input.content@sequentialId}}.txt","mimeType":"text/plain"} Save the response output variables in statusUpdate and the response body in responseUpdate, as shown below: The file that will appear in your Drive will look like this: For more information, visit the discussion on the subject in our community or the videos on our channel. 😃 Related articles Service History Custom Breaks How to review a contact's chat history How to Save Data from Human Service How to Send SMS via API