[JAVA] Make facebook messenger with talk API of api.ai and a3rt
Overview
- Use api.ai for other parts that you want to return fixed messages such as "What is your name?" And "What is your date of birth?" Free conversation is left to a3rt talkAPI.
- Use api.ai's webhook to return weather / news using external API during specific conversations.
- Since it is troublesome this time, it is installed empty. Prepare only the box.
- Implement webhook in java.
api.ai
agent
- The agent is the bot itself. .. I think. ..
- Setting the name, language, and time zone is not difficult.
- This time, I changed the agent name to
kunipon
. Lol
intent
- Intent is a box that classifies users such as "what do you want to know?" And "what kind of emotions" by registering what the user is likely to say.
- You can register multiple responses to user conversations and return them randomly.
- Register the following this time
Intent name |
Registration conversation example |
response |
acquaintance |
"What is your name?" "Who are you?" |
This is kunipon |
age |
"How old" "What is your age?" |
9 years old |
teach_me |
"Please tell me" "What is the weather in Tokyo tomorrow?" |
* This will be described later |
entity
- Used to identify the essence of the text registered in the intent.
- For example, this time I want to determine if I really want to know the weather / news, so to specify "Tell me the weather tomorrow" or "Tell me the news today" in the "teach_me" intent. Register the entity.
- Multiple items and synonyms for items can be registered in one entity.
- It does not have to be synonymous depending on the application.
- This time, I registered as follows.
Entity name |
Reference name |
Synonym example |
teach_me |
weather |
"Weather" "Sunny" "Hot" |
teach_me |
news |
"News" "Events" "Topics" |
intent2
- After determining that the user's conversation belongs to the teach_me intent, you must determine what the user is in the teach_me state. Therefore, an entity can be associated with the intent.
- If the associated entity does not exist in the conversation, a response different from the default response can be returned.
- This time, for example, when the user says "Please tell me", it is set to return the response "I can check the weather or news, but which one do you want?"
- As shown below, the taught_me entity of the table without Action item has a liquid check, and the response is registered in prompts.
Try it once
- Since the word "weather" registered as an entity is included, you can see that the parameter teach_me is weather.
default fallback
- This guy reacts when a conversation that does not match the registered intent comes.
- action is decided by myself, but is default input.unknown?
webhook
- Used when you want to acquire data via api at a specific intent and return the acquired data as a response.
- First, enable the webhook on the fulfillment page and set the URL.
- You can't register localhost etc. as the URL, and you get angry to set a public URL, but at this point you don't have to rush to use heroku or anything. I mean, if you give heroku, you can't debug. .. Google also tells me to use ngrok during development.
Local fulfillment testing with ngrok
- ngrok can be installed with
brew cask install ngrok
, and if you do something like ngrok http 8080
, you can create a public URL like Forwarding https://abcd1234.ngrok.io-> localhost: 8080
.
- After that, go to the target intent page, click Fulfillment at the bottom, and check the "Use webhook" that appears.
- Check with teach_me intent (to bring information with api) and default fallback (to send to a3rt talkAPI when unknown in api.ai)
facebook messenger
- Regarding the integration with api.ai, there is a document at here.
- You can talk like this.
Finally
- The sample of github will be helpful.
- The actual source. I hope it helps someone. ..