0

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 :

  1. User : Hey Google, talk to ScoopMessages
  2. Assistant : Welcome, who do you want to send a message
  3. User : Bob
  4. 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?

codeMan
  • 5,730
  • 3
  • 27
  • 51

1 Answers1

1

There are a lot of parts involved in what you're asking, so lets break it down into these parts:

  1. Authenticating the user
  2. Getting the contact information
  3. Disambiguating contacts
  4. Sending the message

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.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Thanks for this very insightful answer. Clears up a lot of things. I was hoping that the Contact selection would run on users device and assistant would take care of giving the contact phone number/email id to the fulfillment intent where it could be further processed to send the actual message.. – codeMan May 02 '20 at 12:15
  • What is user could spell out the contact number tro whom they have to send the message, I suppose that is not a very good UX, right? – codeMan May 02 '20 at 12:16
  • As I said - fulfillment does not run on the device. Not all devices have the capabilities to "send the message". (Smart speakers don't, for example.) – Prisoner May 02 '20 at 12:28
  • 1
    Spelling out the contact number would be a very bad UX, but doesn't really change the design significantly. You still need to figure out how to send the message. – Prisoner May 02 '20 at 12:32
  • I have figured out the part where I could send a message if I have the phone number. I can do it via a webhook using something like a twillio API. I am more interested to know if there is a simple way to get the user share a contact to my skill – codeMan May 02 '20 at 12:42
  • Is there a way to talk to a GDE like yourself in a more personal forum and share the usecase that I am trying to implement and get suggestion on how to go about it without sharing my usecase/idea on a public forum? – codeMan May 02 '20 at 12:47
  • If an answer has helped you, accepting or upvoting it is appreciated. The [official reddit forum](https://www.reddit.com/r/GoogleAssistantDev/) is a good place to find other developers to more casually chat and connect with. My personal policy is that I'm happy to provide free advice in a public forum, since the entire community then benefits, as I have benefited from community contributions, and I do professional consulting privately. – Prisoner May 02 '20 at 12:59
  • What is the complexity you see in using the People API? – Prisoner May 02 '20 at 12:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212966/discussion-between-codeman-and-prisoner). – codeMan May 02 '20 at 13:13