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
            // verify context
            XCTAssertNotEqual(response.context.conversationID, "")
            // verify entities
            // verify intents
            // verify output
            XCTAssertEqual(response.output.text, response1)
            XCTAssertEqual(response.output.nodesVisited!, nodes1)
            context = response.context

    So, you can access your context variable using:

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