How can I grab the context variable mid conversation and use it in my swift program?

So I'm writing a chatbot application which requires the intake of parameters and then uses these parameters in post requests sent via a payload.

I'm having problems with grabbing the context value from a context variable within swift and was wondering how I would go about grabbing the value of the context variable and executing an action based on the value of that said context.

An example of this would be the following dialog flow...

Me: Trigger this

Bot: Ok, give me param x

Me: x

Bot: Ok I have x param, will post job now

This is the kind of flow I want to happen in the background of my application under the hood but I'm not sure how to grab value x after my user has input it.

1 answer

  • answered 2017-12-06 01:19 Sayuri Mizuguchi

    So, suppose that you are using the iOS SDK from Watson Developer Cloud.

    In your Conversation, add in your node:

    {
      "context": {
        "myVariable": "<? input.text ?>"
      },
      "output": {
        "text": {
          "values": [
            "My context variable value is $myVariable."
          ],
          "selection_policy": "sequential"
        }, { "etc": "etc" }
    

    Obs.: The input.text will capture all that user types, you need to use regex for extract exactly what you want, try to see my examples in this answer.

    And, in the iOS SDK you can see this follow example:

    func testMessage() {
        let description1 = "Start a conversation."
        let expectation1 = self.expectation(description: description1)
    
        let response1 = ["Hi. It looks like a nice drive today. What would you like me to do?"]
        let nodes1 = ["node_1_1467221909631"]
    
        var context: Context?
        conversation.message(workspaceID: workspaceID, failure: failWithError) {
            response in
    
            // verify input
            XCTAssertNil(response.input?.text)
    
            // verify context
            XCTAssertNotNil(response.context.conversationID)
            XCTAssertNotEqual(response.context.conversationID, "")
            XCTAssertNotNil(response.context.system)
            XCTAssertNotNil(response.context.system.additionalProperties)
            XCTAssertFalse(response.context.system.additionalProperties.isEmpty)
    
            // verify entities
            XCTAssertTrue(response.entities.isEmpty)
    
            // verify intents
            XCTAssertTrue(response.intents.isEmpty)
    
            // verify output
            XCTAssertTrue(response.output.logMessages.isEmpty)
            XCTAssertEqual(response.output.text, response1)
            XCTAssertEqual(response.output.nodesVisited!, nodes1)
    
            context = response.context
            expectation1.fulfill()
        }
    

    So, you can access your context variable using:

    context.myVariable
    response.context.myVariable
    
    • See more about methods in Watson Conversation here.
    • iOS SDK from Watson Developer Cloud.