Skip to content
Xinrui Wang edited this page Dec 10, 2017 · 5 revisions

Natual language Process

For detailed explanation for Dialogflow, please click here.

The following only deals with the self-defined Dialogflow service using Kotlin.

You should put your access token into the app\src\main\res\values\string.xml

    <string name="NLP_key">****</string>

class NaturalLanguageProcessService : Service()

OnCreate

This function call initService() to initiate the service.

private fun initService()

This function initiates service and configures the language as well as the connection between android application and the server.

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int):Int

  • Parameters:
    • intent(Intent): This intent consists of message and the receiver of the message. For clarification, contact could be a cellphone number or a slack channel ID.
      //This is for sending message to the NLP service in other
      // activities or services
      val i = Intent(this, NaturalLanguageProcessService::class.java)
              i.putExtra("msg", msg)
              i.putExtra("phonenumber",channel)
              startService(i)

fun sendRequest(queryString: String,eventString: String = "",contextString: String = "")

This function sends the message to Dialogflow server.

  • Parameters:
    • queryString(String): The message sent to Dialogflow.
    • eventString(String): The event configured in Dialogflow.
    • contextString(String): The context con figured in Dialogflow.

private fun onResult(response: AIResponse)

This listener retrieves the message from the server and convert the response to JSON before extracting and broadcasting needed information.

  • Parameters:
    • response(AIResponse): The response received from the server.

private fun clear()

This function clears all the recorded response parameter for fear of interference of next message request.

fun sendtoSlack(msg:String, channel:String)

This function sends message to a certain Slack channel by starting Slack service.

  • Parameters:
    • msg(String): The message to be sent to Slack
    • channel(Sring): Slack channel ID

private fun sendMessage()

This function broadcasts the processing result locally and Central Hub will get notified of the result. This function is called in private fun onResult(response: AIResponse).