-
Notifications
You must be signed in to change notification settings - Fork 589
Integration Firebase Functions #108
Changes from 3 commits
aa516b2
ad096da
2a0b27f
bbd9f4e
a4f9ba8
b33d644
6ce8859
0f862d4
2e38050
c3c2b78
bba436b
b168253
e7d5487
9dcf0d1
9ddd430
1c2b79f
462db73
c8371eb
20b798a
d31ecb6
cafff50
6b8e4bd
4dd38b9
73edd3f
12e2d39
26599e8
32020e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| *.iml | ||
| .gradle | ||
| /local.properties | ||
| /.idea/workspace.xml | ||
| /.idea/libraries | ||
| .DS_Store | ||
| /build | ||
| /captures | ||
| .externalNativeBuild | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Dialogflow Sample | ||
|
|
||
| This app demonstrates how to make gRPC connections to the [Dialogflow API](https://cloud.google.com/dialogflow-enterprise/) | ||
|
|
||
| The app demonstrates how to detect intents: | ||
| - Via Text | ||
| - Via Streaming Audio | ||
| - With Sentiment Analysis | ||
| - With Text-to-Speech | ||
| - With Knowledge Connectors | ||
|
|
||
| To call the Dialogflow API from Android, you need to get authorization tokens from Firebase Cloud Messaging for them to be accepted by the Dialogflow API. To get this token, this sample uses a Firebase Function (in Node.js) to generate these tokens on the behalf of a service account to be used by the app when making a request to the Dialogflow API. | ||
|
|
||
| ## Prerequisites | ||
| - An Android device or emulator | ||
| - Android Studio 3 or later | ||
|
|
||
| ## Setup | ||
| - Create a project (or use an existing one) in the [Google Cloud Console][cloud-console] | ||
| - Enable the [Dialogflow API](https://console.cloud.google.com/apis/library/dialogflow.googleapis.com). | ||
| - Enable the [IAM Service Account Credentials API](https://pantheon.corp.google.com/apis/library/iamcredentials.googleapis.com). | ||
| - [Enable billing][billing]. | ||
| - Be sure that you have gone through the steps by expanding the [Create an agent](https://cloud.google.com/dialogflow-enterprise/docs/quickstart-console#create-an-agent) to create and configure your stopwatch agent. | ||
| - [Import the Dialogflow Agent](https://dialogflow.com/docs/agents/export-import-restore#import) using the `StopwatchAgent.zip` which is located in the `stopwatch` directory. | ||
nnegrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - [Create a Service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts) with the following IAM role: `Dialogflow API Client`. Example name: `dialogflow-client`. ([For more info on: how to add roles to a Service Account](https://cloud.google.com/iam/docs/granting-roles-to-service-accounts#granting_access_to_a_service_account_for_a_resource)) | ||
| - Enable beta features for: | ||
| - [Sentiment Analysis](https://cloud.google.com/dialogflow-enterprise/docs/sentiment#enable_beta_features) | ||
| - [Text-to-Speech](https://cloud.google.com/dialogflow-enterprise/docs/detect-intent-tts#enable_beta_features) | ||
| - [Knowledge Connectors](https://cloud.google.com/dialogflow-enterprise/docs/knowledge-connectors#enable_beta_features) | ||
nnegrey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### Setup the app | ||
| -Clone this repository `git clone https://github.com/GoogleCloudPlatform/android-docs-samples.git` | ||
| - Replace PROJECT_ID in AppController.java with your Project ID | ||
nnegrey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### Setup Firebase on the application: | ||
| - Complete the steps for [Add Firebase to your app](https://firebase.google.com/docs/android/setup) and expand the "Create a Firebase project" section for instructions on how to add project to your Firebase console. Note: No need to complete any other sections, they are already done. | ||
nnegrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - In the [Firebase console](https://console.firebase.google.com/), open the "Authentication" section under Develop. | ||
| - On the **Sign-in Methods** page, enable the **Anonymous** sign-in method. | ||
nnegrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ### Setup and Deploy the Firebase Function | ||
| The Firebase Function provides auth tokens to your app, You'll be using a provided sample function to be run with this app. | ||
|
|
||
| - Follow the steps in this [guide](https://firebase.google.com/docs/functions/get-started) for: | ||
| - "1. Set up Node.js and the Firebase CLI" | ||
| - "2. Initialize Firebase SDK for Cloud Functions". | ||
nnegrey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - Replace `index.js` file with the [provided index.js](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/functions/dialogflow/functions/index.js). | ||
| - Open `index.js`, go to function "generateAccessToken", and replace “SERVICE-ACCOUNT-NAME@YOUR_PROJECT_ID.iam.gserviceaccount.com” with your Service account name (`dialogflow-client`) and project id. | ||
| - Deploy getOAuthToken method by running command: | ||
| ``` | ||
| firebase deploy -—only functions | ||
nnegrey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
| - For your "App Engine Default Service Account" add the following IAM role: `Service Account Token Creator` . ([For more info on: how to add roles to a Service Account](https://cloud.google.com/iam/docs/granting-roles-to-service-accounts#granting_access_to_a_service_account_for_a_resource)) | ||
|
|
||
| - For more info please refer (https://firebase.google.com/docs/functions/get-started). | ||
|
|
||
| ## Run the app | ||
| - You are now ready to build and run the project. In Android Studio you can do this by clicking the 'Play' button in the toolbar. This will launch the app on the emulator or on the device you've selected. | ||
| - As soon the app launches, it will ask for the google sign-in. | ||
| - After successful signing in, choose the option by selecting a checkbox and click on chat button | ||
| - Type the message to send and click on the send button on the bottom right. | ||
nnegrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - Alternatively tap on the mic button to speak and send the message to the Dialog Flow. | ||
nnegrey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| [cloud-console]: https://console.cloud.google.com | ||
| [git]: https://git-scm.com/ | ||
| [android-studio]: https://developer.android.com/studio | ||
| [billing]: https://console.cloud.google.com/billing?project=_ | ||
| [Firebase]: https://firebase.google.com/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| apply plugin: 'com.android.application' | ||
|
|
||
| android { | ||
| compileSdkVersion 28 | ||
| defaultConfig { | ||
| applicationId "com.google.cloud.examples.dialogflow" | ||
| minSdkVersion 26 | ||
| targetSdkVersion 28 | ||
| versionCode 1 | ||
| versionName "1.0" | ||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
| multiDexEnabled true | ||
| } | ||
| buildTypes { | ||
| release { | ||
| minifyEnabled true | ||
| proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
| } | ||
| } | ||
| packagingOptions { | ||
| exclude 'META-INF/DEPENDENCIES' | ||
| exclude 'META-INF/LICENSE' | ||
| exclude 'META-INF/LICENSE.txt' | ||
| exclude 'META-INF/license.txt' | ||
| exclude 'META-INF/NOTICE' | ||
| exclude 'META-INF/NOTICE.txt' | ||
| exclude 'META-INF/notice.txt' | ||
| exclude 'META-INF/ASL2.0' | ||
| exclude 'META-INF/INDEX.LIST' | ||
| exclude 'META-INF/io.netty.versions.properties' | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
|
|
||
| configurations.all { | ||
| resolutionStrategy.force 'com.android.support:support-annotations:25.3.0' | ||
| } | ||
| implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
| implementation 'androidx.appcompat:appcompat:1.0.0' | ||
| implementation 'com.google.android.material:material:1.0.0' | ||
| implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | ||
| testImplementation 'junit:junit:4.12' | ||
| androidTestImplementation 'androidx.test:runner:1.1.0' | ||
| androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' | ||
|
|
||
| implementation 'com.google.firebase:firebase-core:17.0.0' | ||
| implementation 'com.google.firebase:firebase-auth:18.0.0' | ||
| implementation 'com.firebaseui:firebase-ui-auth:4.1.0' | ||
| implementation 'com.google.firebase:firebase-messaging:19.0.1' | ||
| implementation 'com.google.firebase:firebase-functions:18.0.0' | ||
| implementation 'com.android.volley:volley:1.1.1' | ||
| implementation 'com.google.cloud:google-cloud-dialogflow:0.96.0-alpha' | ||
| implementation group: 'io.grpc', name: 'grpc-okhttp', version: '1.21.0' | ||
| implementation group: 'io.grpc', name: 'grpc-netty', version: '1.21.0' | ||
|
|
||
| } | ||
|
|
||
|
|
||
| apply plugin: 'com.google.gms.google-services' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Add project specific ProGuard rules here. | ||
| # You can control the set of applied configuration files using the | ||
| # proguardFiles setting in build.gradle. | ||
| # | ||
| # For more details, see | ||
| # http://developer.android.com/guide/developing/tools/proguard.html | ||
|
|
||
| # If your project uses WebView with JS, uncomment the following | ||
| # and specify the fully qualified class name to the JavaScript interface | ||
| # class: | ||
| #-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
| # public *; | ||
| #} | ||
|
|
||
| # Uncomment this to preserve the line number information for | ||
| # debugging stack traces. | ||
| #-keepattributes SourceFile,LineNumberTable | ||
|
|
||
| # If you keep the line number information, uncomment this to | ||
| # hide the original source file name. | ||
| #-renamesourcefileattribute SourceFile |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.google.cloud.examples.dialogflow; | ||
|
|
||
| import android.content.Context; | ||
| import androidx.test.InstrumentationRegistry; | ||
| import androidx.test.runner.AndroidJUnit4; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| /** | ||
| * Instrumented test, which will execute on an Android device. | ||
| * | ||
| * @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
| */ | ||
| @RunWith(AndroidJUnit4.class) | ||
| public class ExampleInstrumentedTest { | ||
| @Test | ||
| public void useAppContext() throws Exception { | ||
| // Context of the app under test. | ||
| Context appContext = InstrumentationRegistry.getTargetContext(); | ||
|
|
||
| assertEquals("com.example.mlapitest", appContext.getPackageName()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| xmlns:tools="http://schemas.android.com/tools" | ||
| package="com.google.cloud.examples.dialogflow"> | ||
|
|
||
| <uses-permission android:name="android.permission.RECORD_AUDIO" /> | ||
| <uses-permission android:name="android.permission.INTERNET" /> | ||
|
|
||
| <application | ||
| android:name="com.google.cloud.examples.dialogflow.AppController" | ||
| android:allowBackup="true" | ||
| android:icon="@mipmap/ic_launcher" | ||
| android:label="@string/app_name" | ||
| android:roundIcon="@mipmap/ic_launcher" | ||
| android:supportsRtl="true" | ||
| android:theme="@style/AppTheme"> | ||
| <activity android:name="com.google.cloud.examples.dialogflow.ui.ChatActivity" /> | ||
| <activity android:name="com.google.cloud.examples.dialogflow.ui.WelcomeActivity"> | ||
| <intent-filter> | ||
| <action android:name="android.intent.action.MAIN" /> | ||
| <action android:name="android.intent.action.VIEW" /> | ||
|
|
||
| <category android:name="android.intent.category.LAUNCHER" /> | ||
| </intent-filter> | ||
| </activity> | ||
| <activity android:name="com.google.cloud.examples.dialogflow.ui.MainActivity" /> | ||
| <service | ||
| android:name="com.google.cloud.examples.dialogflow.service.MyFirebaseCloudMessagingService" | ||
| android:exported="false"> | ||
| <intent-filter> | ||
| <action android:name="com.google.firebase.MESSAGING_EVENT" /> | ||
| </intent-filter> | ||
| </service> | ||
| </application> | ||
|
|
||
| </manifest> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.google.cloud.examples.dialogflow; | ||
|
|
||
| import android.app.Application; | ||
|
|
||
| import com.google.firebase.functions.FirebaseFunctions; | ||
|
|
||
| import java.text.ParseException; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Date; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class AppController extends Application { | ||
|
|
||
| public static final String TOKEN_RECEIVED = "TOKEN_RECEIVED"; | ||
| public static final String PROJECT_ID = "PROJECT_ID"; | ||
| public static final String SESSION_ID = "sessionId"; | ||
| public static String firebaseInstanceId = ""; | ||
|
|
||
| public static String token = ""; | ||
| public static Date exipryTime; | ||
|
|
||
| /** | ||
| * function to call the firebase function which will send the fcm message containing token and expiry time to the device | ||
| */ | ||
| public static void callFirebaseFunction() { | ||
| Map<String, String> data = new HashMap<>(); | ||
| data.put("deviceId", AppController.firebaseInstanceId); | ||
|
|
||
| FirebaseFunctions firebaseFunctions; | ||
| firebaseFunctions = FirebaseFunctions.getInstance(); | ||
|
|
||
| firebaseFunctions | ||
| .getHttpsCallable("getOAuthToken") | ||
| .call(data); | ||
| } | ||
|
|
||
| /** | ||
| * function to convert string to date | ||
| * @param dt : string date | ||
|
||
| * @return : converted date object | ||
| */ | ||
| public static Date getDateFromString(String dt) { | ||
| SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); | ||
| try { | ||
| Date date = format.parse(dt); | ||
| return date; | ||
| } catch (ParseException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package com.google.cloud.examples.dialogflow.adapter; | ||
|
|
||
| import android.content.Context; | ||
| import androidx.recyclerview.widget.RecyclerView; | ||
| import android.view.LayoutInflater; | ||
| import android.view.View; | ||
| import android.view.ViewGroup; | ||
| import android.widget.RelativeLayout; | ||
| import android.widget.TextView; | ||
|
|
||
| import com.google.cloud.examples.dialogflow.R; | ||
| import com.google.cloud.examples.dialogflow.model.ChatMsgModel; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class ChatRecyclerViewAdapter extends RecyclerView.Adapter<ChatRecyclerViewAdapter.MyViewHolder> { | ||
|
|
||
| private ArrayList<ChatMsgModel> chatMsgModels; | ||
| private Context context; | ||
|
|
||
| public ChatRecyclerViewAdapter(Context context, ArrayList<ChatMsgModel> chatMsgModels) { | ||
|
||
| this.context = context; | ||
| this.chatMsgModels = chatMsgModels; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
| View itemView = LayoutInflater.from(parent.getContext()) | ||
| .inflate(R.layout.custom_chat_recyclerview_item, parent, false); | ||
|
|
||
| return new MyViewHolder(itemView); | ||
| } | ||
|
|
||
| @Override | ||
| public void onBindViewHolder(MyViewHolder holder, final int position) { | ||
| final ChatMsgModel chatMsgModel = chatMsgModels.get(position); | ||
|
|
||
| if(chatMsgModel.getType()==1) { // Message Sent | ||
| holder.tvMsgSent.setText(chatMsgModel.getMsg()); | ||
| holder.tvMsgSent.setVisibility(View.VISIBLE); | ||
| holder.tvMsgReceived.setVisibility(View.GONE); | ||
| } else { | ||
| holder.tvMsgReceived.setText(chatMsgModel.getMsg()); | ||
| holder.tvMsgReceived.setVisibility(View.VISIBLE); | ||
| holder.tvMsgSent.setVisibility(View.GONE); | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public int getItemCount() { | ||
| return chatMsgModels.size(); | ||
| } | ||
|
|
||
| public class MyViewHolder extends RecyclerView.ViewHolder { | ||
| RelativeLayout rlMain; | ||
| TextView tvMsgSent; | ||
| TextView tvMsgReceived; | ||
|
|
||
| public MyViewHolder(View view) { | ||
| super(view); | ||
| rlMain = view.findViewById(R.id.rlMain); | ||
| tvMsgSent = view.findViewById(R.id.tvMsgSent); | ||
| tvMsgReceived = view.findViewById(R.id.tvMsgReceived); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.google.cloud.examples.dialogflow.model; | ||
|
|
||
| public class ChatMsgModel { | ||
|
|
||
| private String msg; | ||
| private int type; | ||
|
|
||
| public ChatMsgModel(String msg, int type) { | ||
| this.msg = msg; | ||
| this.type = type; | ||
| } | ||
|
|
||
| public String getMsg() { | ||
| return msg; | ||
| } | ||
|
|
||
| public void setMsg(String msg) { | ||
|
||
| this.msg = msg; | ||
| } | ||
|
|
||
| public int getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public void setType(int type) { | ||
|
||
| this.type = type; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.