Implement the Smart Reply API

This is all the code we need for making this work:

Let’s now go through it step by step…

The conversation variable is an Array of FirebaseTextMessages. It is basically what we’ll feed to Firebase in order for it to give us back the smart replies!

A FirebaseTextMessage is composed of:

The text message (duh)

(duh) The time the text was sent

the was sent The id of the user that sent the text

of the that sent the text A boolean, defining if the user who wrote the text is the local user (if that’s the case, there will be no need to define the id of the user since it will be auto-generated by Firebase)

Next, we have the addMessage() function that creates a new FirebaseTextMessage and adds it to the conversation Array

Since this is just an example and we are not actually implementing a real online chat system, we’ll never define a local user but instead, our conversation will only consist of remote users, something like this:

When the user writes a message, they’ll be able to also fill in their name, which will be the id of that user.

In a real app scenario, be sure to use randomly generated IDs for each user or, more simply, if you plan on implementing user authentication anyway, you can use the userId of Firebase Authentication (I have made a separate article on that here).

Finally, the getHints() function will send the conversation to Firebase, where it will be processed. As you can see, the success listener will return a result SmartReplySuggestionResult object that will also indicate whether the operation was successful.

We are yet to add any UI to make our application work, so, for now, don’t worry about the errorText or the hintButton variables on the code snippet above, since they’ll be implemented next.

Sprinkle in a bit of UI

Let’s navigate to our activity_main.xml file and let’s write this code:

Nothing too fancy:

EditText for the ID of the user [ID = nameText]

for the [ID = nameText] EditText for the text message [ID = messageText]

for the [ID = messageText] Button to send the message [ID = sendButton] (add it to the conversation variable)

to [ID = sendButton] (add it to the conversation variable) Button to process the conversation and display the hints [ID = hintsButton]

to [ID = hintsButton] Button to clear the conversation [ID = clearButton] (clear the conversation variable)

to [ID = clearButton] (clear the conversation variable) 3 Buttons , which are the suggested replies [IDs = hint0Button, hint1Button, hint2Button]

, which are the [IDs = hint0Button, hint1Button, hint2Button] EditText to print any error that may occur when retrieving the hints [ID = errorText]

Let’s get back into our MainActivity.kt file in the OnCreate() function to add the logic for these elements:

As always, let’s take this step by step:

When clicked, the send button will just add a text onto the conversation Array, using the messageText EditText for the text’s message

The hintsButton, when clicked, will just run the getHints() function (shown above), which also makes the suggestion buttons visible

The clearButton, instead, will make the suggestion buttons invisible again and will clear the conversation Array

The suggestion buttons, when clicked, will add the new suggested reply to the conversation Array

Finally, if you are one of those copy/paste guys, here’s the entire MainActivity.kt class for you:

$ PROFIT $

Alright, let’s test our app and see if it works:

Before you go…

You can find all of the code used in this article on my Github, here

I have also made a Youtube video version of this article, you can find it here