abhi007tyagi / Android_Dialogflow_Chatbot_Library

Android Library to easily integrate Dialogflow based chatbots into an existing application with Chat screen.
Other
12 stars 6 forks source link

Android Dialogflow ChatbotLibrary

Android Library to easily integrate Dialogflow based chatbots into an existing application with Chat screen.

Steps

1. Add dependencies and other entries to your project's gradle

dependencies


// Chatbot - Java V2
    implementation 'com.tyagiabhinav:androiddialogflowchatbot:<latest version>' //0.1.9
    implementation 'com.google.cloud:google-cloud-dialogflow:2.2.0'
    implementation 'io.grpc:grpc-okhttp:1.31.1'

packaging options under android tag


// Chatbot - Java V2
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/INDEX.LIST'
    }

2. Add ChatbotActivity to your projects Manifest file


<activity
            android:name="com.tyagiabhinav.dialogflowchatlibrary.ChatbotActivity"
            android:launchMode="singleTask"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

3. Add Google Credential JSON file for Dialogflow Project

Generate a Google Credential JSON file for your Dialogflow Agent and save it under res->raw->

4. Call ChatbotActivity


public void openChatbot(View view) {
        // provide your Dialogflow's Google Credential JSON saved under RAW folder in resources
        DialogflowCredentials.getInstance().setInputStream(getResources().openRawResource(R.raw.test_agent_credentials));

        ChatbotSettings.getInstance().setChatbot( new Chatbot.ChatbotBuilder()
//                .setDoAutoWelcome(false) // True by Default, False if you do not want the Bot to greet the user Automatically. Dialogflow agent must have a welcome intent to handle this
//                .setChatBotAvatar(getDrawable(R.drawable.avatarBot)) // provide avatar for your bot if default is not required
//                .setChatUserAvatar(getDrawable(R.drawable.avatarUser)) // provide avatar for your the user if default is not required
                .setShowMic(true) // False by Default, True if you want to use Voice input from the user to chat
                .build());
        Intent intent = new Intent(<CALLING ACTIVITY>.this, ChatbotActivity.class);
        Bundle bundle = new Bundle();

        // provide a UUID for your session with the Dialogflow agent
        bundle.putString(ChatbotActivity.SESSION_ID, UUID.randomUUID().toString());
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.putExtras(bundle);
        startActivity(intent);
    }

5. Replace default colours

In your application, add following colours and replace the default values


    <color name="userAvatarBG">#666666</color>
    <color name="chatPrimary">#CC0000</color>
    <color name="chatPrimaryFocus">#DD0000</color>
    <color name="chatPrimaryPressed">#A60000</color>
    <color name="chatPrimaryDisabled">#66cc0000</color>
    <color name="chatBGnText">#FFFFFF</color>
    <color name="chatSecondary">#FFFFFF</color>
    <color name="chatSecondaryFocus">#F1F1F1</color>
    <color name="chatSecondaryPressed">#E9E9E9</color>
    <color name="chatSecondaryDisabled">#66FFFFFF</color>
    <color name="botBGBubbleStroke">#FFCCCC</color>
    <color name="botBGBubble">#FFCCCC</color>
    <color name="userBGBubbleStroke">#CAE4F5</color>
    <color name="userBGBubble">#CAE4F5</color>
    <color name="checkbox">#161616</color>
    <color name="statusBarColor">@color/chatPrimaryPressed</color>

6. Add proguard rules to your project (if you don't get response from Chatbot)


-keep public class com.google.a** {
  public protected *;
}

7. Interacting with webhook to show simple text, text message with buttons, checkboxes or navigating to other activities

a. Sample Dialogflow Webhook NodeJS code

Get the sample NodeJS webhook code from the SampleDialogflowWebhook

b. Dialogflow Intent to capture events from Android app

event setup in Dialogflow Intent

webhook enable for Intent

c. Intents to send UI interaction with Android

Enable webhook fulfillment for all these intents

webhook enable for Intent

Code to send from webhook

    const params = <Depending on Interaction Type Change params here>;
    const param_context = {name: "param_context", lifespan: 10, parameters: params};
    agent.context.set(param_context);
    agent.add('This is a message section for showing text'); // Text message to be shown to the user