株式会社ユーザーローカル様から、事前申し込みに登録しておいた「ユーザーローカル人工知能ボットAPI」のご用意が整いました! とメールを頂きました。早速遊んでみました。
検証した環境
・AndroidStudio 2.1.1
・Android 4.0.4(ふっ、古い...)
キーの取得
ユーザーローカル社から頂いたメールに記載されています。メールには簡単な使い方、ドキュメントへのリンクなども。
#アプリ作成
###プロジェクトの新規作成
1.File > New > New Project
。Application Name は適当に。
2.Target Device は Phone and Table の API15:Android 4.0.3(IceCreamSandwich)を選択しました。
3.Activity Mode は Empty Activity 、Finish
###プログラム
ネットにアクセスするのでAndroidManifest に パーミッションを追加
<uses-permission android:name="android.permission.INTERNET" />
ボタンを押した時の処理などを記述していきます。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 追加ボタン
Button addButton = (Button) findViewById(R.id.addButton);
addButton.setOnClickListener(this);
}
public void onClick(View v) {
EditText msgEditText = (EditText) findViewById(R.id.msgEditText);
String msg = msgEditText.getText().toString();
TextView msgTextView = (TextView)findViewById(R.id.msgTextView);
msgTextView.append("send:" + msg + "\n\n");
if (msg != null && msg.length() > 0) {
AsyncGetMsg atClass = new AsyncGetMsg(this);
atClass.setOnCallBack(new AsyncGetMsg.CallBackTask() {
@Override
public void CallBack(String result) {
super.CallBack(result);
// 取得したメッセージを追加
TextView msgTextView = (TextView)findViewById(R.id.msgTextView);
msgTextView.append("ret:" + result + "\n\n");
EditText msgEditText = (EditText) findViewById(R.id.msgEditText);
msgEditText.setText("");
}
});
// AsyncTaskの実行
atClass.execute(msg);
}
}
}
Http要求は別スレッドで実行が必要なのでAsyncTaskで作成します。KEYにはメールで頂いたキーを設定します。URLを組み立てたらHttpURLConnectionでボットAPIに要求し、レスポンスをStringBuilderに取得します。
応答はJSON形式です(例:{"status":"success","result":"野球観戦つながり?"}
)。JSONObjectを作成して、getStringでresultを取得します。
public class AsyncGetMsg extends AsyncTask<String, Void, String> {
private static String TAG = "AsyncGetMsg";
private static String KEY = "頂いたキーを記入";
private ProgressDialog progressDialog_;
private Context mContext;
private CallBackTask callbacktask;
public AsyncGetMsg(Context context) {
super();
this.mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog_ = new ProgressDialog(mContext);
progressDialog_.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog_.show();
}
@Override
protected String doInBackground(String... params) {
String mMsg = "";
HttpURLConnection connection = null;
try {
// URLの組み立て
Log.d(TAG, "params[0]:" + params[0]);
String message = URLEncoder.encode(params[0],"utf-8");
Log.d(TAG, "message:" + message);
URL url = new URL("https://chatbot-api.userlocal.jp/api/chat?key=" + KEY + "&message=" + message);
// リクエスト
connection = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
connection.disconnect();
// 取得した文字列からjsonobjectを作成
JSONObject jsonObject = new JSONObject(sb.toString());
mMsg = jsonObject.getString("result");
Log.d(TAG, "status:" + jsonObject.getString("status"));
Log.d(TAG, "result:" + jsonObject.getString("result"));
} catch (Exception e) {
mMsg = "Exception:" + e.getMessage();
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return mMsg;
}
// doInBackgroundの事後処理(UIスレッド)
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog_.dismiss();
callbacktask.CallBack(result);
}
public void setOnCallBack(CallBackTask _cbj) {
callbacktask = _cbj;
}
public static class CallBackTask {
public void CallBack(String result) {
}
}
}
UI。必要最低限です。。。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="horizontal">
<EditText
android:id="@+id/msgEditText"
android:layout_width="0dip"
android:layout_weight="8"
android:layout_height="wrap_content"
android:inputType="text"/>
<Button
android:id="@+id/addButton"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="投稿"/>
</LinearLayout>
<TextView
android:id="@+id/msgTextView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
動かしてみます。
うどんと天ぷらとか、天気の話とか、会話が成立すると面白いです。応答も毎回同じではなくちょっと変えてきます。これからもっと精度が上がっていくのでしょう。
SNSと組み合わせたり、pepperに話させたりすると面白いかもしれません。
現在第二期を募集しているようです。