Tag:

google

    Audio to Text by Google Speech API in Salesforce Lightning

    Recently we were having a requirement for getting text from audio or video file.  These audios were majorly from customer calls with agents.  Once we get text from audio files , we can review those conversions. We can check how agents are discussing with customers. This will increase customer happiness.

    There are many solutions available for converting audio/video file to text. Some of major solutions are below

    1. Google Speech API 
    2. Amazon Transcribe
    3. Microsoft Speech API
    4. Watson Speech to Text

    This post is using Google Speech API to transcribe audio file into text. This audio file is attached in case record.  As a requirement, we need to convert attached audio file and add that to case comment in same record.

    To make this requirement achievable, we have to create two lightning component. One component will get access token from Google authorization service and second component will use this access token to generate text from audio file.

    Prerequisite for this solution:

    1. Create user in Google cloud at https://cloud.google.com/speech-to-text/
    2. Create project in Google Cloud and enable Cloud Speech-to-Text API at  https://console.cloud.google.com/apis/library
    3. Authorize domain in OAuth Consent Screen. Use Lightning url of your org without https.
    4. Create credential for this API. We need to set redirect url which will be lighting component url for authenticating and getting access token.

    As mentioned above, to get transcription from audio we have to create two lightning components.

    1. First Lightning Component to get Access Token from Google Authentication Service
    2. Second Lightning Component to get Transcription from Speech API

    1. Get Access Token from Google Authentication Service

    Let us Authenticate Salesforce App and get access token from Google authentication service. Below steps will be used to get access token

    1. Create Apex class to generate URL for getting access token
    2. Create lightning component to get access token.
    3. Create custom metadata to store access token for later user.
    4. Make lightning component as tab
    a. Create Apex class to generate URL for getting access token

    Create apex class GoogleAuthService to get authentication url. This url will generate token after you authenticate using google credential created in step #1.

    createAuthURL – This apex method will create authentication redirect url.

    getAccessToken: This apex method will generate access token. This token will be saved in custom metadata for further use.

    b. Create Lightning component to get access token

    GoogleAuthComponent component is created for getting access token. It will use above mentioned GoogleAuthService class to generate token.


    c. Create custom metadata to store access token for later user

    Create one custom metadata type named GoogleAuthSetting and add field AccessToken in it. Add class MetadataService to update access token value. This class will be called from class GoogleAuthService.

    d. Make lightning component as tab

    Create a tab for this lightning component so that it will be accessible easily. Url of this tab should be added in step 3 of prerequisites.

    2. Get Transcription from Speech API

    Let us create another lightning component. This component will take case id and generate transcribed text from any attached audio file on specific record. This transcribed text is added in case comment.


    As we have access token from first lightning component into custom metadata.  Let us use that access token from metadata to create transcribe text. Apex class GoogleSpeechService will generate translated text using https://speech.googleapis.com/v1p1beta1/speech:recognize api.

    Add this component to case page layout.

    Demo Time :

    References:

    https://cloud.google.com/speech-to-text

    January 31, 2020 2 comments
    2 Facebook Twitter Google + Pinterest