PubNub Integration

Google Natural Language API

Bots & AI

Bots & AI

Chat & Social

Chat & Social

External APIs

External APIs

In-app chat

In-app chat

Multiuser Spaces

Multiuser Spaces

Last Updated:

2020/05/08

Author:

PubNub, Inc.

APISign-upDocumentation

Insightful Text Analysis

Derive insights from unstructured text using Google machine learning. Natural Language uses machine learning to reveal the structure and meaning of text. You can extract information about people, places, and events, and better understand social media sentiment and customer conversations. Natural Language enables you to analyze text and also integrate it with your document storage on Cloud Storage.

Google Natural Language API

Setup:

  1. Sign-up for a Google Cloud account and setup the Natural Language API.

  2. Sign-up for a PubNub account, if you don't have one already.

  3. Import the Integration Block into your PubNub account. You can choose to add this to an existing application or create a new one.

  4. Configure the Integration Block module code, connecting it to your Google Cloud account:

    1. Create a Secret in PubNub Vault, named GOOGLE_CLOUD_NLP_SERVICES_APIKEY, with value equal to your Google API Key for this service, retrieved as part of step 1 above.

  5. Start the Integration Block module.

  6. Try a test message (you can click on Publish to do this).

Walkthrough:

This Integration Block is setup to listen to a specific PubNub channel (CHANNEL-to-text-google-analyze) but you can configure it to listen to all channels or a subset, using wildcards, e.g. chat.*. Thus, you can add comprehension even in private chats and other secure communication channels. The Integration Block is flexible in that it can be used both as part of a PubNub messaging flow, i.e. as part of publishing data, as well as accessed separately through a REST endpoint. Additionally, customization of capabilities can be done through publish message metadata or REST query parameters. In publish metadata, the customization properties are set as part of a JSON object specific for the function named google-text-analyze-v1.

  1. TEXT_LOCATION_ID: json dot notation for locating the text field in the message payload.

  2. INPUT_TEXT_TYPE: direct configuration of the Google Natural Language API property

  3. INPUT_TEXT_LANGUAGE: direct configuration of the Google Natural Language API property

  4. INPUT_TEXT_ENCODING_TYPE: direct configuration of the Google Natural Language API property

  5. OUTPUT_LOCATION_ID: json dot notation for locating the output data in the final message payload. Specify meta if you want the output data to be part of the metadata property of the message payload.

Input:

The below payload would be sent via an actual publish API call, via one of PubNub's 70+ SDKs.

{
	"message": {
		"content": {
			"text": "I am feeling rather good."
		}
	},
	"channel": "CHANNEL-to-translate-Google",
	"meta": {
		"functions": {
			"google-translate-v2": {
				"input": {
					"TEXT_LOCATION_ID": "content.text"
				}
			}
		}
	}
}

Output:

In this example, the updated content is part of the published message itself. The Integration Block is setup to do an Before Publish or Signal execution, which means that incoming message payloads will be forwarded to the Google Cloud Natural Language API, analyzed and then updated.

{
	"content": {
		"text": "I am feeling rather good."
	},
	"sentiment": {
		"google": {
			"documentSentiment": {
				"magnitude": 0.9,
				"score": 0.9
			},
			"language": "en",
			"sentences": [{
				"text": {
					"content": "I am feeling rather good.",
					"beginOffset": 0
				},
				"sentiment": {
					"magnitude": 0.9,
					"score": 0.9
				}
			}]
		}
	}
}

Feedback & Support

Need help? Contact PubNub Support.


Talk to an expert

Contact Sales