VidyasagarMSC/WatBot

Send Data from Android to BlueMix

nizamudeenms opened this issue · 2 comments

Hi,
Your Code is very useful to create a bot application, thanks for sharing.
Am unable to find a way to send the data from Android Application to Blue Mix server.

How am trying to send the value from android application.


private void sendMessage() {

        final String inputmessage = this.inputMessage.getText().toString().trim();
        if (!this.initialRequest) {
            Message inputMessage = new Message();
            inputMessage.setMessage(inputmessage);
            inputMessage.setId("1");
//Trying to set values for context here
            inputMessage.setFines("999");
            messageArrayList.add(inputMessage);
        } else {
            Message inputMessage = new Message();
            inputMessage.setMessage(inputmessage);
            inputMessage.setId("100");

            this.initialRequest = false;
            Toast.makeText(getApplicationContext(), "Tap on the message for Voice", Toast.LENGTH_LONG).show();

        }

        this.inputMessage.setText("");

        mAdapter.notifyDataSetChanged();

        Thread thread = new Thread(new Runnable() {
            public void run() {
                try {
                    ConversationService service = new ConversationService(ConversationService.VERSION_DATE_2017_02_03);
                    service.setUsernameAndPassword("senate", "password");

                    context.put("1",inputmessage);

                    MessageRequest newMessage = new MessageRequest.Builder().inputText(inputmessage).context(context).build();

                    MessageResponse response = service.message("workspace", newMessage).execute();

                    //Passing Context of last conversation
                    if (response.getContext() != null) {
                        context = response.getContext();
                    }
                    Message outMessage = new Message();
                    if (response != null) {
                        if (response.getOutput() != null && response.getOutput().containsKey("text")) {
                            ArrayList responseList = (ArrayList) response.getOutput().get("text");
                            Log.i("responseList", responseList.get(0).toString());
                            if (null != responseList && responseList.size() > 0) {
                                outMessage.setMessage((String) responseList.get(0));
                                context = response.getContext();
//Trying to set values for context here
                                outMessage.setFines("200");
                                outMessage.setId("2");
                            }
                            messageArrayList.add(outMessage);
                        }

                        runOnUiThread(new Runnable() {
                            public void run() {
                                mAdapter.notifyDataSetChanged();
                                if (mAdapter.getItemCount() > 1) {
                                    recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, null, mAdapter.getItemCount() - 1);
                                }
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }


I want evaluate this reply using $var in Blue Mix conversation and respond accordingly.

@nizamudeenms What data are you referring to? Can you highlight the line number in the code?

I mean my own data retrieved from Database

EG: in node js you send data like this ....


var ConversationV1 = require('watson-developer-cloud/conversation/v1');

var conversation = new ConversationV1({
  username: 'username',
  password: 'password', 
  path: { workspace_id: 'workspaceID' },  
  version_date: '2017-31-05'
});

var context; 

conversation.message({}, processResponse);


function processResponse(err, response) {
  if (err) {
    console.error(err); 
    return;
  }
  
  if (response.output.text.length != 0) {
      	console.log(response.output.text[0]);
//	console.log(response);

	if( response.output.text[0].indexOf("Checking traffic fines") > -1 || response.output.text[0].indexOf("checking traffic fines") > -1 ) {
		console.log("You have 600 Qatari Riyals of Traffic Fines");
		context = response.context;
		context.fines = "yes";

	 	conversation.message(
		{
			input: {},
			context: context
		}
		,processResponse
		);			
	}

	context = response.context; 
  }
}

var stdin = process.openStdin();

stdin.addListener("data", function(d) {
    // note:  d is an object, and when converted to a string it will
    // end with a linefeed.  so we (rather crudely) account for that  
    // with toString() and then trim() 
    console.log("you entered: [" + 
        d.toString().trim() + "]");

    conversation.message(

		{
  			input: {text: d.toString().trim()},
			context: context 
		}

		, processResponse); 

  });

In the above code, this block is validating if user has fines, appends 'fines' variable to context and sends 'fines' variable in the Blumix server. If I run this node js code in local console its giving the the proper output but I don't know how to convert this code to java.

if( response.output.text[0].indexOf("Checking traffic fines") > -1 || response.output.text[0].indexOf("checking traffic fines") > -1 ) {
console.log("You have 600 Qatari Riyals of Traffic Fines");
context = response.context;
context.fines = "yes";
conversation.message(
{
input: {},
context: context
}
,processResponse
);

In my case the fines are retrieved from database and sent to blue mix.
I want to know how to send data 'fines' from java ... It should be read by blue mix....

Please tell me how can I append a variable 'fine' in java context and send it to blue mix in java.

It will better if you give me your mobile number to call you. . I have mailed you please share mobile no in mail...