Dialogflow Chatbot Code Empowerherwellness
Dialogflow Chatbot Code Empowerherwellness
package com.example.empowerherwellness;
import android.content.Context;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.dialogflow.v2.SessionsClient;
import com.google.cloud.dialogflow.v2.SessionsSettings;
import java.io.InputStream;
DialogflowHelper
package com.example.empowerherwellness
import android.content.Context
import android.util.Log
import com.google.auth.oauth2.GoogleCredentials
import com.google.cloud.dialogflow.v2.*
import java.io.InputStream
import java.util.concurrent.Executors
object DialogflowHelper {
fun getDialogflowSession(context: Context): SessionsClient? {
return try {
val stream: InputStream = context.assets.open("empowerherbot-wcib-62a9a7bb39b0.json")
val credentials = GoogleCredentials.fromStream(stream)
val sessionsSettings = SessionsSettings.newBuilder()
.setCredentialsProvider { credentials }
.build()
SessionsClient.create(sessionsSettings)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
MainActivity.kt
package com.example.empowerherwellness
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// Initialize views
userInput = findViewById(R.id.userInput)
sendButton = findViewById(R.id.sendButton)
botResponse = findViewById(R.id.botResponse)
startButton = findViewById(R.id.start_button) // Initialize here
activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/userInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:padding="12dp"
android:hint="Type your message..."
android:textSize="16sp"/>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:layout_gravity="center"
android:padding="10dp"
android:marginTop="10dp"/>
<TextView
android:id="@+id/botResponse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bot Response will appear here..."
android:textSize="16sp"
android:padding="10dp"
android:layout_marginTop="10dp"/>
</LinearLayout>