Reputation: 5758
I am trying to add Google Assistant conversational feature for my messaging app.
I am stuck in letting user pick a contact from his contact list to send the message to.
Consider the following example :
Assistant : There are multiple Bob, Select who do you want to send a message to?
Shows all the contacts with bob names in them
I have implemented the 1 and 2 conversation, but I am not able to implement 3 and 4. I am not finding any blogs/source where it shows how to select a contact. Is this even possible? Please point me to an example or let me know how I can build this functionality?
Upvotes: 0
Views: 174
Reputation: 50701
There are a lot of parts involved in what you're asking, so lets break it down into these parts:
Authenticating the user
As a first step, you'll need to know who the user is, so you can look up their contacts. For this, you'll need to use Account Linking so their Assistant Account can be linked to the account that you're maintaining for them. Although you want to use their Google contacts - you don't automatically get access to this through the Assistant.
Related to this, you'll need an access token to call the Google APIs to get their contacts, and this access token needs to have specific authorization to access the scope you need. The latter part is tricker - Account Linking alone won't do this for you, and you aren't able to get their authorization by voice alone. The easiest approach is to use "Google Sign In for Assistant and OAuth" scheme together. This answer on StackOverflow provides some guidance on how that will work.
Getting the contact information
With the auth token, in the Intent Hander where they give a name you're able to make a query against the user's contacts on their behalf. Here, however, you run into two competing APIs from Google, each with their own quirks.
The Contacts API is an older protocol, and the results are returned in XML using their older gData format, but it offers a way to do queries. The People API is newer, and returns data in JSON format, but does not offer a way to query the fields. The People API officially replaces the Contacts API, which "will be deprecated in the future".
With the Contacts API, you can do a query against a term which will search all the text fields in the user's contacts. To use "Bob" from your example, this might look something like
GET https://www.google.com/m8/feeds/contacts/default/full?q=Bob
With the People API, however, you need to fetch the entire list of their contacts using the people.connections.list method, likely specifying you want the "names" and "nicknames" fields. With this list, you can then search through the fields for matches.
If there is one match, you're good.
If there is more than one match however...
Disambiguating contacts
...you'll have to reply to the user requesting this. If there are a limited number, you can probably say something like "Who do you want to send it to? Bob Smith or Joe Bob?"
If there are more, however, you might prompt with how many "Bob"s there are, and show a list or carousel if the user is on a device that supports visual lists. But keep in mind that you can't rely on this - they may be on a voice-only device, or may not have a way to enter via screen.
Sending a message
You don't indicate how you're sending a message, but keep in mind that fulfillment does not run on the user's device - it runs at a webhook you control somewhere. This is a case where you may be using their auth token again to send the message.
Upvotes: 1