Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. import com.github.kittinunf.fuel.core.FuelError;
  2. import com.github.kittinunf.fuel.core.Handler;
  3. import com.github.kittinunf.fuel.core.Request;
  4. import com.github.kittinunf.fuel.core.Response;
  5. import com.ibm.watson.developer_cloud.conversation.v1.ConversationService;
  6. import com.ibm.watson.developer_cloud.conversation.v1.model.MessageRequest;
  7. import com.ibm.watson.developer_cloud.conversation.v1.model.MessageResponse;
  8. import com.ibm.watson.developer_cloud.http.ServiceCallback;
  9.  
  10. import java.util.HashMap;
  11. import java.util.Map;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15.     private static final String TAG = "MainActivity";
  16.     private ConversationService myConversationService = null;
  17.     private TextView chatDisplayTV;
  18.     private EditText userStatementET;
  19.     private final String IBM_USERNAME = "abcde-1234-1234-abcde-abcdefghijk";
  20.     private final String IBM_PASSWORD = "abcdefghijk";
  21.     private final String IBM_WORKSPACE_ID = "xyzxyzxyz-1234-1234-5678-abcdefgixyz123";
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.         chatDisplayTV = findViewById(R.id.tv_chat_display);
  28.         userStatementET = findViewById(R.id.et_user_statement);
  29.  
  30.         //instantiating IBM Watson Conversation Service
  31.         myConversationService =
  32.                 new ConversationService(
  33.                         "2017-12-06",
  34.                         IBM_USERNAME,
  35.                         IBM_PASSWORD
  36.                 );
  37.  
  38.         userStatementET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
  39.             @Override
  40.             public boolean onEditorAction(TextView tv, int action, KeyEvent keyEvent) {
  41.                 if (action == EditorInfo.IME_ACTION_DONE) {
  42.                     //show the user statement
  43.                     final String userStatement = userStatementET.getText().toString();
  44.                     chatDisplayTV.append(
  45.                             Html.fromHtml("<p><b>YOU:</b> " + userStatement + "</p>")
  46.                     );
  47.                     userStatementET.setText("");
  48.  
  49.                     MessageRequest request = new MessageRequest.Builder()
  50.                             .inputText(userStatement)
  51.                             .build();
  52.                     // initiate chat conversation
  53.                     myConversationService
  54.                             .message(IBM_WORKSPACE_ID, request)
  55.                             .enqueue(new ServiceCallback<MessageResponse>() {
  56.                                 @Override
  57.                                 public void onResponse(MessageResponse response) {
  58.                                     final String botStatement = response.getText().get(0);
  59.                                     runOnUiThread(new Runnable() {
  60.                                         @Override
  61.                                         public void run() {
  62.                                             chatDisplayTV.append(
  63.                                                     Html.fromHtml("<p><b>BOT:</b> " +
  64.                                                             botStatement + "</p>")
  65.                                             );
  66.                                         }
  67.                                     });
  68.  
  69.                                     // if the intent is joke then we access the third party
  70.                                     // service to get a random joke and respond to user
  71.                                     if (response.getIntents().get(0).getIntent().endsWith("Joke")) {
  72.                                         final Map<String, String> params = new HashMap<String, String>() {{
  73.                                             put("Accept", "text/plain");
  74.                                         }};
  75.                                         Fuel.get("https://icanhazdadjoke.com/").header(params)
  76.                                                 .responseString(new Handler<String>() {
  77.                                                     @Override
  78.                                                     public void success(Request request, Response response, String body) {
  79.                                                         Log.d(TAG, "" + response + " ; " + body);
  80.                                                         chatDisplayTV.append(
  81.                                                                 Html.fromHtml("<p><b>BOT:</b> " +
  82.                                                                         body + "</p>")
  83.                                                         );
  84.                                                     }
  85.  
  86.                                                     @Override
  87.                                                     public void failure(Request request, Response response, FuelError fuelError) {
  88.                                                     }
  89.                                                 });
  90.                                     }
  91.                                 }
  92.  
  93.                                 @Override
  94.                                 public void onFailure(Exception e) {
  95.                                     Log.d(TAG, e.getMessage());
  96.                                 }
  97.                             });
  98.                 }
  99.                 return false;
  100.             }
  101.         });
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement