├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── drawable
│ │ │ │ ├── in_message_bg.9.png
│ │ │ │ └── out_message_bg.9.png
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ └── layout
│ │ │ │ ├── list_item_chat_message.xml
│ │ │ │ └── activity_chat.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── directlineex
│ │ │ │ └── brsingh
│ │ │ │ └── directlineandroidexample
│ │ │ │ ├── ChatMessage.java
│ │ │ │ ├── ChatAdapter.java
│ │ │ │ └── ChatActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── directlineex
│ │ │ └── brsingh
│ │ │ └── directlineandroidexample
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── directlineex
│ │ └── brsingh
│ │ └── directlineandroidexample
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── images
├── devbot1.JPG
└── devbot2.JPG
├── .idea
├── copyright
│ └── profiles_settings.xml
├── vcs.xml
├── libraries
│ ├── android_android_25.xml
│ ├── javax_inject_1.xml
│ ├── jsr305_2_0_1.xml
│ ├── javawriter_2_1_1.xml
│ ├── hamcrest_library_1_3.xml
│ ├── hamcrest_integration_1_3.xml
│ ├── javax_annotation_api_1_2.xml
│ ├── support_v4_25_1_0.xml
│ ├── play_services_base_8_4_0.xml
│ ├── junit_4_12.xml
│ ├── play_services_basement_8_4_0.xml
│ ├── play_services_appindexing_8_4_0.xml
│ ├── hamcrest_core_1_3.xml
│ ├── support_annotations_25_1_0.xml
│ ├── transition_25_1_0.xml
│ ├── espresso_core_2_2_2.xml
│ ├── support_vector_drawable_25_1_0.xml
│ ├── animated_vector_drawable_25_1_0.xml
│ ├── espresso_idling_resource_2_2_2.xml
│ ├── exposed_instrumentation_api_publish_0_5.xml
│ ├── rules_0_5.xml
│ ├── design_25_1_0.xml
│ ├── runner_0_5.xml
│ ├── appcompat_v7_25_1_0.xml
│ ├── support_compat_25_1_0.xml
│ ├── recyclerview_v7_25_1_0.xml
│ ├── support_core_ui_25_1_0.xml
│ ├── support_fragment_25_1_0.xml
│ ├── support_core_utils_25_1_0.xml
│ └── support_media_compat_25_1_0.xml
├── modules.xml
├── runConfigurations.xml
├── compiler.xml
├── gradle.xml
└── misc.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── LICENSE
├── gradlew.bat
├── README.md
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/images/devbot1.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brijrajsingh/DirectLineAndroidSample/HEAD/images/devbot1.JPG
--------------------------------------------------------------------------------
/images/devbot2.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brijrajsingh/DirectLineAndroidSample/HEAD/images/devbot2.JPG
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brijrajsingh/DirectLineAndroidSample/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/drawable/in_message_bg.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brijrajsingh/DirectLineAndroidSample/HEAD/app/src/main/res/drawable/in_message_bg.9.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brijrajsingh/DirectLineAndroidSample/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brijrajsingh/DirectLineAndroidSample/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brijrajsingh/DirectLineAndroidSample/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/out_message_bg.9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brijrajsingh/DirectLineAndroidSample/HEAD/app/src/main/res/drawable/out_message_bg.9.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brijrajsingh/DirectLineAndroidSample/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/brijrajsingh/DirectLineAndroidSample/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/.idea/libraries/android_android_25.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/libraries/javax_inject_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/libraries/jsr305_2_0_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/libraries/javawriter_2_1_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/libraries/hamcrest_library_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/libraries/hamcrest_integration_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/libraries/javax_annotation_api_1_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DirectLineAndroidExample
3 |
4 |
5 | Your Message
6 | Your Chat history shows up here
7 | Send Message
8 | Sendmsg
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/libraries/support_v4_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/libraries/play_services_base_8_4_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/libraries/junit_4_12.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/libraries/play_services_basement_8_4_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/libraries/play_services_appindexing_8_4_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/test/java/com/directlineex/brsingh/directlineandroidexample/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.directlineex.brsingh.directlineandroidexample;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/libraries/hamcrest_core_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/libraries/support_annotations_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # Intellij
36 | *.iml
37 | .idea/workspace.xml
38 |
39 | # Keystore files
40 | *.jks
41 |
--------------------------------------------------------------------------------
/.idea/libraries/transition_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/libraries/espresso_core_2_2_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/libraries/support_vector_drawable_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/libraries/animated_vector_drawable_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\brsingh\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/.idea/libraries/espresso_idling_resource_2_2_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/libraries/exposed_instrumentation_api_publish_0_5.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/libraries/rules_0_5.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/.idea/libraries/design_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/libraries/runner_0_5.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/libraries/appcompat_v7_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/libraries/support_compat_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/libraries/recyclerview_v7_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/libraries/support_core_ui_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/libraries/support_fragment_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/libraries/support_core_utils_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/libraries/support_media_compat_25_1_0.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/directlineex/brsingh/directlineandroidexample/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.directlineex.brsingh.directlineandroidexample;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.directlineex.brsingh.directlineandroidexample", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.2"
6 | defaultConfig {
7 | applicationId "com.directlineex.brsingh.directlineandroidexample"
8 | minSdkVersion 18
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.1.0'
28 | compile 'com.android.support:design:25.1.0'
29 | testCompile 'junit:junit:4.12'
30 | compile 'com.google.android.gms:play-services-appindexing:8.4.0'
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/java/com/directlineex/brsingh/directlineandroidexample/ChatMessage.java:
--------------------------------------------------------------------------------
1 | package com.directlineex.brsingh.directlineandroidexample;
2 |
3 | /**
4 | * Created by brsingh on 1/6/2017.
5 | */
6 | public class ChatMessage {
7 | private long id;
8 | private boolean isMe;
9 | private String message;
10 | private Long userId;
11 | private String dateTime;
12 |
13 | public long getId() {
14 | return id;
15 | }
16 | public void setId(long id) {
17 | this.id = id;
18 | }
19 | public boolean getIsme() {
20 | return isMe;
21 | }
22 | public void setMe(boolean isMe) {
23 | this.isMe = isMe;
24 | }
25 | public String getMessage() {
26 | return message;
27 | }
28 | public void setMessage(String message) {
29 | this.message = message;
30 | }
31 | public long getUserId() {
32 | return userId;
33 | }
34 |
35 | public void setUserId(long userId) {
36 | this.userId = userId;
37 | }
38 |
39 | public String getDate() {
40 | return dateTime;
41 | }
42 |
43 | public void setDate(String dateTime) {
44 | this.dateTime = dateTime;
45 | }
46 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
9 |
15 |
18 |
19 |
20 |
21 |
22 |
24 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/list_item_chat_message.xml:
--------------------------------------------------------------------------------
1 |
4 |
10 |
11 |
18 |
19 |
28 |
29 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DirectLineAndroidSample
2 | Android Sample for Direct Line API - Microsoft Bot Framework
3 |
4 | This android sample showcases the REST API usage of Direct Line API endpoints provided by the Microsoft Bot framework https://docs.botframework.com/en-us/restapi/directline3/
5 |
6 | There are 4 steps in a chat conversations.
7 |
8 | 1. Authentication - you get a primary token from the dev.botframework.com. to get the token go to dev.botframework.com, enable the Direct Line connector, and click on Edit.
9 | 
10 | 
11 |
12 | 2. Connect to the API and start the conversation, see the startConversation() function. Sends the Primary Token and receives secondary token,conversationId that you will use in your subsequent calls to Direct Line API.
13 |
14 | 3. Send the Chat Message to Bot - sendMessageToBot(String messageText) function. This function uses the conversationId and the token received in the first authentication call and sends an Activity to the bot. In this sample we are using an activity of type "message", a message can also contain one or multiple attachments.
15 |
16 | 4. Receiving response from Bot - Responses can be fetched from Bot by polling a GET API or use the web sockets. In this sample we have used the polling approach. function pollBotResponses - polls the GET API for conversation updates, the bot sends a JSON representation that looks like this
17 |
18 | `{
19 | "activities": [
20 | {
21 | "type": "message",
22 | "id": "61S2RSXVCQYKf034GmZMoT|0000001",
23 | "timestamp": "2017-01-07T12:03:38.7462471Z",
24 | "channelId": "directline",
25 | "from": {
26 | "id": "user1"
27 | },
28 | "conversation": {
29 | "id": "61S2RSXVCQYKf034GmZMoT"
30 | },
31 | "text": "i would like to complain about a product "
32 | },
33 | {
34 | "type": "message",
35 | "id": "61S2RSXVCQYKf034GmZMoT|0000002",
36 | "timestamp": "2017-01-07T12:03:42.045473Z",
37 | "channelId": "directline",
38 | "from": {
39 | "id": "formbot",
40 | "name": "FormBot"
41 | },
42 | "conversation": {
43 | "id": "61S2RSXVCQYKf034GmZMoT"
44 | },
45 | "text": "Gee! really sorry to hear that, can you please give me the order id that looks like CRNXXXX, i would lodge a complaint in the system and someone will call you back.",
46 | "replyToId": "61S2RSXVCQYKf034GmZMoT|0000001"
47 | }
48 | ],
49 | "watermark": "2"
50 | }`
51 |
52 | this functions polls the GET API using the runnable handler which polls every 5 seconds.
53 |
54 | ## Configuration Parameters - AndroidManifest.xml file
55 | Replace the values with Relevant values
56 |
57 | `
58 | `
59 |
60 |
61 | Things to do :
62 |
63 | 1. The calls are synchronous in nature, need to make them asynchronous.
64 |
65 | 2. Prepare a SDK for Android Direct Line API.
66 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_chat.xml:
--------------------------------------------------------------------------------
1 |
12 |
13 |
19 |
20 |
27 |
28 |
37 |
38 |
51 |
52 |
60 |
61 |
68 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/directlineex/brsingh/directlineandroidexample/ChatAdapter.java:
--------------------------------------------------------------------------------
1 | package com.directlineex.brsingh.directlineandroidexample;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.view.Gravity;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.BaseAdapter;
10 | import android.widget.LinearLayout;
11 | import android.widget.RelativeLayout;
12 | import android.widget.TextView;
13 |
14 | import java.util.List;
15 |
16 | /**
17 | * Created by brsingh on 1/6/2017.
18 | */
19 | public class ChatAdapter extends BaseAdapter {
20 |
21 | private final List chatMessages;
22 | private Activity context;
23 |
24 | public ChatAdapter(Activity context, List chatMessages) {
25 | this.context = context;
26 | this.chatMessages = chatMessages;
27 | }
28 |
29 | @Override
30 | public int getCount() {
31 | if (chatMessages != null) {
32 | return chatMessages.size();
33 | } else {
34 | return 0;
35 | }
36 | }
37 |
38 | @Override
39 | public ChatMessage getItem(int position) {
40 | if (chatMessages != null) {
41 | return chatMessages.get(position);
42 | } else {
43 | return null;
44 | }
45 | }
46 |
47 | @Override
48 | public long getItemId(int position) {
49 | return position;
50 | }
51 |
52 | @Override
53 | public View getView(final int position, View convertView, ViewGroup parent) {
54 | ViewHolder holder;
55 | ChatMessage chatMessage = getItem(position);
56 | LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
57 |
58 | if (convertView == null) {
59 | convertView = vi.inflate(R.layout.list_item_chat_message, null);
60 | holder = createViewHolder(convertView);
61 | convertView.setTag(holder);
62 | } else {
63 | holder = (ViewHolder) convertView.getTag();
64 | }
65 |
66 | boolean myMsg = chatMessage.getIsme() ;//Just a dummy check
67 | //to simulate whether it me or other sender
68 | setAlignment(holder, myMsg);
69 | holder.txtMessage.setText(chatMessage.getMessage());
70 | holder.txtInfo.setText(chatMessage.getDate());
71 |
72 | return convertView;
73 | }
74 |
75 | public void add(ChatMessage message) {
76 | chatMessages.add(message);
77 | }
78 |
79 | public void add(List messages) {
80 | chatMessages.addAll(messages);
81 | }
82 |
83 | private void setAlignment(ViewHolder holder, boolean isMe) {
84 | if (!isMe) {
85 | holder.contentWithBG.setBackgroundResource(R.drawable.in_message_bg);
86 |
87 | LinearLayout.LayoutParams layoutParams =
88 | (LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams();
89 | layoutParams.gravity = Gravity.RIGHT;
90 | holder.contentWithBG.setLayoutParams(layoutParams);
91 |
92 | RelativeLayout.LayoutParams lp =
93 | (RelativeLayout.LayoutParams) holder.content.getLayoutParams();
94 | lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
95 | lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
96 | holder.content.setLayoutParams(lp);
97 | layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams();
98 | layoutParams.gravity = Gravity.RIGHT;
99 | holder.txtMessage.setLayoutParams(layoutParams);
100 |
101 | layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams();
102 | layoutParams.gravity = Gravity.RIGHT;
103 | holder.txtInfo.setLayoutParams(layoutParams);
104 | } else {
105 | holder.contentWithBG.setBackgroundResource(R.drawable.out_message_bg);
106 |
107 | LinearLayout.LayoutParams layoutParams =
108 | (LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams();
109 | layoutParams.gravity = Gravity.LEFT;
110 | holder.contentWithBG.setLayoutParams(layoutParams);
111 |
112 | RelativeLayout.LayoutParams lp =
113 | (RelativeLayout.LayoutParams) holder.content.getLayoutParams();
114 | lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
115 | lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
116 | holder.content.setLayoutParams(lp);
117 | layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams();
118 | layoutParams.gravity = Gravity.LEFT;
119 | holder.txtMessage.setLayoutParams(layoutParams);
120 |
121 | layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams();
122 | layoutParams.gravity = Gravity.LEFT;
123 | holder.txtInfo.setLayoutParams(layoutParams);
124 | }
125 | }
126 |
127 | private ViewHolder createViewHolder(View v) {
128 | ViewHolder holder = new ViewHolder();
129 | holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage);
130 | holder.content = (LinearLayout) v.findViewById(R.id.content);
131 | holder.contentWithBG = (LinearLayout) v.findViewById(R.id.contentWithBackground);
132 | holder.txtInfo = (TextView) v.findViewById(R.id.txtInfo);
133 | return holder;
134 | }
135 |
136 | private static class ViewHolder {
137 | public TextView txtMessage;
138 | public TextView txtInfo;
139 | public LinearLayout content;
140 | public LinearLayout contentWithBG;
141 | }
142 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/directlineex/brsingh/directlineandroidexample/ChatActivity.java:
--------------------------------------------------------------------------------
1 | package com.directlineex.brsingh.directlineandroidexample;
2 |
3 | import android.content.Context;
4 | import android.content.pm.ApplicationInfo;
5 | import android.content.pm.PackageManager;
6 | import android.net.Uri;
7 | import android.os.Debug;
8 | import android.os.Handler;
9 | import android.os.StrictMode;
10 | import android.support.v7.app.AppCompatActivity;
11 | import android.os.Bundle;
12 | import android.text.TextUtils;
13 | import android.util.Log;
14 | import android.view.View;
15 | import android.widget.Button;
16 | import android.widget.EditText;
17 | import android.widget.ListView;
18 | import android.widget.RelativeLayout;
19 | import android.widget.TextView;
20 | import android.widget.Toast;
21 |
22 | import com.google.android.gms.appindexing.Action;
23 | import com.google.android.gms.appindexing.AppIndex;
24 | import com.google.android.gms.appindexing.Thing;
25 | import com.google.android.gms.common.api.GoogleApiClient;
26 |
27 | import org.json.JSONException;
28 | import org.json.JSONObject;
29 |
30 | import java.io.BufferedInputStream;
31 | import java.io.IOException;
32 | import java.io.InputStream;
33 | import java.io.InputStreamReader;
34 | import java.io.OutputStream;
35 | import java.io.Reader;
36 | import java.io.UnsupportedEncodingException;
37 | import java.net.HttpURLConnection;
38 | import java.net.MalformedURLException;
39 | import java.net.URL;
40 | import java.text.DateFormat;
41 | import java.text.SimpleDateFormat;
42 | import java.util.ArrayList;
43 | import java.util.Calendar;
44 | import java.util.Date;
45 |
46 | import static android.R.attr.id;
47 | import static com.directlineex.brsingh.directlineandroidexample.R.layout.activity_chat;
48 |
49 | /**
50 | * A Chat Screen Activity
51 | */
52 | public class ChatActivity extends AppCompatActivity {
53 | private EditText messageET;
54 | private ListView messagesContainer;
55 | private Button sendBtn;
56 | private ChatAdapter adapter;
57 | private ArrayList chatHistory;
58 | private String localToken = "";
59 | private String conversationId = "";
60 | private String primaryToken = "";
61 | private String botName = "";
62 |
63 | //keep the last Response MsgId, to check if the last response is already printed or not
64 | private String lastResponseMsgId = "";
65 |
66 | Handler handler = new Handler();
67 | Runnable runnable = new Runnable() {
68 | public void run() {
69 | pollBotResponses();
70 | }
71 | };
72 |
73 | /**
74 | * ATTENTION: This was auto-generated to implement the App Indexing API.
75 | * See https://g.co/AppIndexing/AndroidStudio for more information.
76 | */
77 | private GoogleApiClient client;
78 |
79 | @Override
80 | protected void onCreate(Bundle savedInstanceState) {
81 | super.onCreate(savedInstanceState);
82 | setContentView(activity_chat);
83 | initControls();
84 |
85 | primaryToken = getMetaData(getBaseContext(),"botPrimaryToken");
86 | botName = getMetaData(getBaseContext(),"botName").toLowerCase();
87 |
88 | // ATTENTION: This was auto-generated to implement the App Indexing API.
89 | // See https://g.co/AppIndexing/AndroidStudio for more information.
90 | client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
91 | runnable.run();
92 | }
93 |
94 | public void pollBotResponses()
95 | {
96 | //Toast.makeText(getBaseContext(),
97 | // "test",
98 | // Toast.LENGTH_SHORT).show();
99 | String botResponse = "";
100 | if(conversationId != "" && localToken != "") {
101 | botResponse = getBotResponse();
102 | if (botResponse != "") {
103 | try {
104 | JSONObject jsonObject = new JSONObject(botResponse);
105 | String responseMsg = "";
106 | Integer arrayLength = jsonObject.getJSONArray("activities").length();
107 | String msgFrom = jsonObject.getJSONArray("activities").getJSONObject(arrayLength-1).getJSONObject("from").get("id").toString();
108 | String curMsgId = jsonObject.getJSONArray("activities").getJSONObject(arrayLength-1).get("id").toString();
109 |
110 | if(msgFrom.trim().toLowerCase().equals(botName)) {
111 | if(lastResponseMsgId == "") {
112 | responseMsg = jsonObject.getJSONArray("activities").getJSONObject(arrayLength - 1).get("text").toString();
113 | AddResponseToChat(responseMsg);
114 | lastResponseMsgId = curMsgId;
115 | }
116 | else if(!lastResponseMsgId.equals(curMsgId))
117 | {
118 | responseMsg = jsonObject.getJSONArray("activities").getJSONObject(arrayLength - 1).get("text").toString();
119 | AddResponseToChat(responseMsg);
120 | lastResponseMsgId = curMsgId;
121 | }
122 | }
123 | } catch (Exception e) {
124 | e.printStackTrace();
125 | }
126 | }
127 | }
128 | handler.postDelayed(runnable, 1000*5);
129 | }
130 |
131 |
132 | private void initControls() {
133 | messagesContainer = (ListView) findViewById(R.id.messagesContainer);
134 | messageET = (EditText) findViewById(R.id.messageEdit);
135 | sendBtn = (Button) findViewById(R.id.chatSendButton);
136 |
137 | TextView meLabel = (TextView) findViewById(R.id.meLbl);
138 | TextView companionLabel = (TextView) findViewById(R.id.friendLabel);
139 | RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
140 | companionLabel.setText("Chat Bot");// Hard Coded
141 | sayHelloToClient();
142 |
143 | sendBtn.setOnClickListener(new View.OnClickListener() {
144 | @Override
145 | public void onClick(View v) {
146 | String messageText = messageET.getText().toString();
147 | if (TextUtils.isEmpty(messageText)) {
148 | return;
149 | }
150 |
151 | ChatMessage chatMessage = new ChatMessage();
152 | chatMessage.setId(122);//dummy
153 | chatMessage.setMessage(messageText);
154 | chatMessage.setDate(DateFormat.getDateTimeInstance().format(new Date()));
155 | chatMessage.setMe(true);
156 |
157 | messageET.setText("");
158 | displayMessage(chatMessage);
159 | new Connection().execute(messageText);
160 |
161 | });
162 | }
163 |
164 | //Get the bot response by polling a GET to directline API
165 | private String getBotResponse() {
166 | //Only for demo sake, otherwise the network work should be done over an asyns task
167 | StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
168 | StrictMode.setThreadPolicy(policy);
169 |
170 | String UrlText = "https://directline.botframework.com/v3/directline/conversations/" + conversationId + "/activities";
171 | URL url = null;
172 | String responseValue = "";
173 |
174 | try {
175 | url = new URL(UrlText);
176 | } catch (MalformedURLException e) {
177 | e.printStackTrace();
178 | }
179 | HttpURLConnection urlConnection = null;
180 | try {
181 | String basicAuth = "Bearer " + localToken;
182 | urlConnection = (HttpURLConnection) url.openConnection();
183 | urlConnection.setRequestProperty("Authorization", basicAuth);
184 | urlConnection.setRequestMethod("GET");
185 | urlConnection.setRequestProperty("Content-Type", "application/json");
186 |
187 | int responseCode = urlConnection.getResponseCode(); //can call this instead of con.connect()
188 | if (responseCode >= 400 && responseCode <= 499) {
189 | throw new Exception("Bad authentication status: " + responseCode); //provide a more meaningful exception message
190 | }
191 | else {
192 | InputStream in = new BufferedInputStream(urlConnection.getInputStream());
193 | responseValue = readStream(in);
194 | Log.w("responseSendMsg ",responseValue);
195 | }
196 | } catch (IOException e) {
197 | e.printStackTrace();
198 | }
199 | catch (Exception e) {
200 | e.printStackTrace();
201 | }
202 | finally {
203 | urlConnection.disconnect();
204 | }
205 |
206 | return responseValue;
207 | }
208 |
209 | //sends the message by making it an activity to the bot
210 | private void sendMessageToBot(String messageText) {
211 | //Only for demo sake, otherwise the network work should be done over an asyns task
212 | StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
213 | StrictMode.setThreadPolicy(policy);
214 |
215 | String UrlText = "https://directline.botframework.com/v3/directline/conversations/" + conversationId + "/activities";
216 | URL url = null;
217 |
218 | try {
219 | url = new URL(UrlText);
220 | } catch (MalformedURLException e) {
221 | e.printStackTrace();
222 | }
223 | HttpURLConnection urlConnection = null;
224 | try {
225 | String basicAuth = "Bearer " + localToken;
226 |
227 | JSONObject jsonObject = new JSONObject();
228 | try {
229 | jsonObject.put("type","message");
230 | jsonObject.put("text",messageText);
231 | jsonObject.put("from",(new JSONObject().put("id","user1")));
232 | } catch (JSONException e) {
233 | e.printStackTrace();
234 | }
235 |
236 | String postData = jsonObject.toString();
237 |
238 | urlConnection = (HttpURLConnection) url.openConnection();
239 | urlConnection.setRequestProperty("Authorization", basicAuth);
240 | urlConnection.setRequestMethod("POST");
241 | urlConnection.setRequestProperty("Content-Type", "application/json");
242 | urlConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
243 | OutputStream out = urlConnection.getOutputStream();
244 | out.write(postData.getBytes());
245 |
246 | int responseCode = urlConnection.getResponseCode(); //can call this instead of con.connect()
247 | if (responseCode >= 400 && responseCode <= 499) {
248 | throw new Exception("Bad authentication status: " + responseCode); //provide a more meaningful exception message
249 | }
250 | else {
251 | InputStream in = new BufferedInputStream(urlConnection.getInputStream());
252 | String responseValue = readStream(in);
253 | Log.w("responseSendMsg ",responseValue);
254 | }
255 | } catch (IOException e) {
256 | e.printStackTrace();
257 | }
258 | catch (Exception e) {
259 | e.printStackTrace();
260 | }
261 | finally {
262 | urlConnection.disconnect();
263 | }
264 |
265 | }
266 |
267 |
268 | //returns the conversationID
269 | private String startConversation()
270 | {
271 | //Only for demo sake, otherwise the network work should be done over an asyns task
272 | StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
273 | StrictMode.setThreadPolicy(policy);
274 |
275 | String UrlText = "https://directline.botframework.com/v3/directline/conversations";
276 | URL url = null;
277 | String responseValue = "";
278 |
279 | try {
280 | url = new URL(UrlText);
281 | } catch (MalformedURLException e) {
282 | e.printStackTrace();
283 | }
284 | HttpURLConnection urlConnection = null;
285 | try {
286 | String basicAuth = "Bearer " + primaryToken;
287 | urlConnection = (HttpURLConnection) url.openConnection();
288 | urlConnection.setRequestProperty("Authorization", basicAuth);
289 | urlConnection.setRequestMethod("POST");
290 | urlConnection.setRequestProperty("Content-Type", "application/json");
291 | } catch (IOException e) {
292 | e.printStackTrace();
293 | }
294 | try {
295 | InputStream in = new BufferedInputStream(urlConnection.getInputStream());
296 | responseValue = readStream(in);
297 | } catch (Exception e) {
298 | e.printStackTrace();
299 | }
300 | finally {
301 | urlConnection.disconnect();
302 | }
303 |
304 | return responseValue;
305 | }
306 |
307 | //read the chat bot response
308 | private String readStream(InputStream in) {
309 | char[] buf = new char[2048];
310 | Reader r = null;
311 | try {
312 | r = new InputStreamReader(in, "UTF-8");
313 | } catch (UnsupportedEncodingException e) {
314 | e.printStackTrace();
315 | }
316 | StringBuilder s = new StringBuilder();
317 | while (true) {
318 | int n = 0;
319 | try {
320 | n = r.read(buf);
321 | } catch (IOException e) {
322 | e.printStackTrace();
323 | }
324 | if (n < 0)
325 | break;
326 | s.append(buf, 0, n);
327 | }
328 |
329 | Log.w("streamValue",s.toString());
330 | return s.toString();
331 | }
332 |
333 |
334 | public void displayMessage(ChatMessage message) {
335 | adapter.add(message);
336 | adapter.notifyDataSetChanged();
337 | scroll();
338 | }
339 |
340 | private void scroll() {
341 | messagesContainer.setSelection(messagesContainer.getCount() - 1);
342 | }
343 |
344 | private void sayHelloToClient() {
345 |
346 | chatHistory = new ArrayList();
347 |
348 | ChatMessage msg = new ChatMessage();
349 | msg.setId(1);
350 | msg.setMe(false);
351 | msg.setMessage("Hello");
352 | msg.setDate(DateFormat.getDateTimeInstance().format(new Date()));
353 | chatHistory.add(msg);
354 |
355 | adapter = new ChatAdapter(ChatActivity.this, new ArrayList());
356 | messagesContainer.setAdapter(adapter);
357 |
358 | for (int i = 0; i < chatHistory.size(); i++) {
359 | ChatMessage message = chatHistory.get(i);
360 | displayMessage(message);
361 | }
362 | }
363 |
364 | /*
365 | Add the bot response to chat window
366 | */
367 | private void AddResponseToChat(String botResponse)
368 | {
369 | ChatMessage message = new ChatMessage();
370 | //message.setId(2);
371 | message.setMe(false);
372 | message.setDate(DateFormat.getDateTimeInstance().format(new Date()));
373 | message.setMessage(botResponse);
374 | displayMessage(message);
375 | }
376 |
377 |
378 | /*
379 | Get metadata from manifest file against a given key
380 | */
381 | public static String getMetaData(Context context, String name) {
382 | try {
383 | ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
384 | Bundle bundle = ai.metaData;
385 | return bundle.getString(name);
386 | } catch (PackageManager.NameNotFoundException e) {
387 | Log.w("Metadata", "Unable to load meta-data: " + e.getMessage());
388 | }
389 | return null;
390 | }
391 |
392 | /**
393 | * ATTENTION: This was auto-generated to implement the App Indexing API.
394 | * See https://g.co/AppIndexing/AndroidStudio for more information.
395 | */
396 | public Action getIndexApiAction() {
397 | Thing object = new Thing.Builder()
398 | .setName("Chat Page") // TODO: Define a title for the content shown.
399 | // TODO: Make sure this auto-generated URL is correct.
400 | .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
401 | .build();
402 | return new Action.Builder(Action.TYPE_VIEW)
403 | .setObject(object)
404 | .setActionStatus(Action.STATUS_TYPE_COMPLETED)
405 | .build();
406 | }
407 |
408 | @Override
409 | public void onStart() {
410 | super.onStart();
411 |
412 | // ATTENTION: This was auto-generated to implement the App Indexing API.
413 | // See https://g.co/AppIndexing/AndroidStudio for more information.
414 | client.connect();
415 | AppIndex.AppIndexApi.start(client, getIndexApiAction());
416 | }
417 |
418 | @Override
419 | public void onStop() {
420 | super.onStop();
421 |
422 | // ATTENTION: This was auto-generated to implement the App Indexing API.
423 | // See https://g.co/AppIndexing/AndroidStudio for more information.
424 | AppIndex.AppIndexApi.end(client, getIndexApiAction());
425 | client.disconnect();
426 | }
427 |
428 | private class Connection extends AsyncTask {
429 |
430 | @Override
431 | protected String doInBackground(String... arg0) {
432 | String conversationTokenInfo = startConversation();
433 | JSONObject jsonObject = null;
434 |
435 | if(conversationTokenInfo != "") {
436 | try {
437 | jsonObject = new JSONObject(conversationTokenInfo);
438 | } catch (JSONException e) {
439 | e.printStackTrace();
440 | }
441 | }
442 |
443 | //send message to bot and get the response using the api conversations/{conversationid}/activities
444 | if(jsonObject != null) {
445 | try {
446 | conversationId = jsonObject.get("conversationId").toString();
447 | localToken = jsonObject.get("token").toString();
448 | } catch (JSONException e) {
449 | e.printStackTrace();
450 | }
451 | }
452 |
453 | if(conversationId != "") {
454 | sendMessageToBot(arg0[0]);
455 | }
456 |
457 | return null;
458 | }
459 |
460 | }
461 | }
462 |
463 |
--------------------------------------------------------------------------------