LINEbot でオウム返し 2017 PHP版

やり方調べるのになぜか時間がかかる.

なんだかつらたん.

ただ, オウム返ししたいだけなのに!

しくみ

LINEサーバを介して自分のサーバ内のスクリプトで受け取ります.

スクリプト作成の前に LINE管理画面で,

- アクセストークンをもらう
- サーバー内スクリプトまでのURLを教えておく

スクリプト

LINE側 API はわかりやすい仕様で作られています.


<?php

$access_token = /* your access token */;

$url = 'https://api.line.me/v2/bot/message/reply';

// receive json data from line webhook
$raw = file_get_contents('php://input');
$receive = json_decode($raw, true);

// parse received events
$event = $receive['events'][0];
$reply_token  = $event['replyToken'];
$message_text = $event['message']['text'];


// build request headers
$headers = array('Content-Type: application/json',
                 'Authorization: Bearer ' . $access_token);

// build request body
$message = array('type' => 'text',
                 'text' => $message_text);

$body = json_encode(array('replyToken' => $reply_token,
                          'messages'   => array($message)));


// post json with curl
$options = array(CURLOPT_URL            => $url,
                 CURLOPT_CUSTOMREQUEST  => 'POST',
                 CURLOPT_RETURNTRANSFER => true,
                 CURLOPT_HTTPHEADER     => $headers,
                 CURLOPT_POSTFIELDS     => $body);

$curl = curl_init();
curl_setopt_array($curl, $options);
curl_exec($curl);
curl_close($curl);

結果

どんだけつらたんなのか!


新しくなった Gradle Tips and Recipes : 各ソースセットの配置の確認

新しくなってます, Gradle Tips and Recipes .

Gradle Tips and Recipes | Android Studio

Gradle が利用するディレクトリ

それぞれ指定できるのはなんとなく知っていたが.


android {
  ...
  sourceSets {
    main {
      java.srcDirs = ['other/java']
    ....

これらの記述無し無指定のデフォルトの場合はどのディレクトリが利用対象になっているのか.

ドキュメントやツールスクリプトなど,

Git に登録して管理はしたいが, アプリ自体に影響ない位置はどこなのか??

と思っていましたが.

Configure Build Variants | Android Studio

右上 [Gradle] タブから以下ダブルクリックで実行.

MyApplication > Tasks > android > sourceSets

Gradle が認識している対象ディレクトリ群が Gradle Console に出力される.


------------------------------------------------------------
Project :app
------------------------------------------------------------

...

debug
----
Compile configuration: compile
build.gradle name: android.sourceSets.debug
Java sources: [app/src/debug/java]
Manifest file: app/src/debug/AndroidManifest.xml
Android resources: [app/src/debug/res]
Assets: [app/src/debug/assets]
AIDL sources: [app/src/debug/aidl]
RenderScript sources: [app/src/debug/rs]
JNI sources: [app/src/debug/jni]
JNI libraries: [app/src/debug/jniLibs]
Java-style resources: [app/src/debug/resources]

これで, AndroidStudio でファイルを操作しながらの Git 管理しても安心です.

他にもあれこれ小技がのっていますよ, このレシピ.


あやしい Project Structure ダイアログを Experimental にしてライブラリ更新を正しく把握する

「かんたんにGUI上で編集する」という考え方なのだろうと思いますが, 今や設定項目の多さや複雑さに対応できているのか疑問に思うダイアログの「Project Structure」.

実行時の挙動が問題ない場合, 設定ファイル自体を直接GUIに対応するように修正する, というのもなんともおかしな話で.

一方, 利用しているライブラリの最新のバージョンを確認したいときはどうしたらいいのか.

Settings > Editor > Inspections > "Newer Library Versions Available"

サード・パーティのライブラリに関してはチェックしていない? の変化なし.

Experimental な機能を使う

Preferences > Build.. > Gradle > Experimental

そして, Project Structure を開くと, ライブラリ同士の依存関係を見ることができたり,

JCenter を利用したライブラリのバージョン確認もできます.

昔は, フツーにチェックしてくれたように思うが, いつからこうなった?


単独で最新版をダウンロードできるようになった platform-tools のURL

これまでは, サイズがでかい Android-SDK 内にあった platform-tools.


~/sdk $ tree -L
.
├── add-ons
│   └── addon-google_apis-google-21
├── build-tools
│   ├── 21.0.0
│   ├── 21.1.0
│   ├── 21.1.1
│   ├── 21.1.2
│   ├── 22.0.0
│   ├── 22.0.1
│   ├── 23.0.0
│   ├── 23.0.1
│   ├── 23.0.2
│   ├── 23.0.3
│   ├── 24.0.0
│   ├── 24.0.1
│   ├── 24.0.2
│   ├── 24.0.3
│   └── 25.0.0
├── docs
│   ├── _redirects.yaml
│   ├── about
│   ├── app.yaml
│   ├── assets
│   ├── auto
│   ├── channels
│   ├── community
│   ├── design
│   ├── develop
│   ├── distribute
│   ├── favicon-a.ico
│   ├── favicon.ico
│   ├── gcm_navtree_data.js
│   ├── gms_navtree_data.js
│   ├── google
│   ├── googlef295f8c7d59b0d29.html
│   ├── guide
│   ├── images
│   ├── index.html
│   ├── jd_collections.js
│   ├── jd_extras.js
│   ├── jd_lists_unified.js
│   ├── jd_tag_helpers.js
│   ├── legal.html
│   ├── license.html
│   ├── navtree_data.js
│   ├── ndk
│   ├── offline.html
│   ├── out
│   ├── package.xml
│   ├── preview
│   ├── reference
│   ├── resources
│   ├── resources.html
│   ├── robots.txt
│   ├── samples
│   ├── sdk
│   ├── security_at_android_dot_com.txt
│   ├── shareables
│   ├── sitemap-intl.txt
│   ├── sitemap.txt
│   ├── source.properties
│   ├── support.html
│   ├── timestamp.js
│   ├── tools
│   ├── training
│   ├── tv
│   └── wear
├── extras
│   ├── android
│   ├── google
│   └── m2repository
├── licenses
│   ├── android-sdk-license
│   └── android-sdk-preview-license
├── patcher
│   ├── v1
│   ├── v3
│   └── v4
├── platform-tools
│   ├── NOTICE.txt
│   ├── adb
│   ├── api
│   ├── dmtracedump
│   ├── etc1tool
│   ├── fastboot
│   ├── hprof-conv
│   ├── lib
│   ├── package.xml
│   ├── source.properties
│   ├── sqlite3
│   └── systrace
├── platforms
│   ├── android-19
│   ├── android-21
│   ├── android-24
│   └── android-25
├── samples
├── sources
│   └── android-24
├── system-images
│   ├── android-21
│   └── android-22
├── temp
│   ├── docs-22_r01.zip
│   ├── docs-22_r01.zip.inf
│   ├── sysimg_x86_64-22_r01.zip
│   └── sysimg_x86_64-22_r01.zip.inf
└── tools
    ├── NOTICE.txt
    ├── android
    ├── ant
    ├── apps
    ├── bin
    ├── bin64
    ├── ddms
    ├── draw9patch
    ├── emulator
    ├── emulator-check
    ├── emulator64-arm
    ├── emulator64-crash-service
    ├── emulator64-mips
    ├── emulator64-x86
    ├── hierarchyviewer
    ├── jobb
    ├── lib
    ├── lib64
    ├── lint
    ├── mksdcard
    ├── monitor
    ├── monkeyrunner
    ├── package.xml
    ├── proguard
    ├── qemu
    ├── screenshot2
    ├── source.properties
    ├── support
    ├── templates
    ├── traceview
    └── uiautomatorviewer

分割されて単独で, Google サーバーからzipで配信されるようになりました.

収録物は以下.


~/platform-tools $ tree -L 1
.
├── NOTICE.txt
├── adb
├── api
├── dmtracedump
├── etc1tool
├── fastboot
├── hprof-conv
├── lib
├── source.properties
├── sqlite3
└── systrace

~/platform-tools $ ./adb version
Android Debug Bridge version 1.0.36
Revision 0e9850346394-android

~/platform-tools $ ./fastboot --version
fastboot version 0e9850346394-android

~/platform-tools $ ./sqlite3 --version
3.9.2 2015-11-02 18:31:45 bda77dda9697c463c3d0704014d51627fceee328

SDKが必要ない場合でも, adb, fastboot, sqlite3 などのバイナリツールを気軽にダウンロードしやすくなります.

platform-tools-latest-windows.zip

platform-tools-latest-darwin.zip

platform-tools-latest-linux.zip

参考 : Ever wanted to download adb or fastboot without having to download the whole of Android Studio?.


Android 6.x (Marshmallow) ホーム長押し (アシスト機能) アプリの設定

ホームボタンを長押しするとアプリが起動するようになったりしています.

ここから起動できるアプリを変更してみます.

OSバージョンで異なるようですが, 今回は, Android OS 6.x でやってみます.

[設定] - [アプリ] とタップしていきます.

ここで, 右上の「歯車」ボタンをタップします.

そして「既定のアプリ」をタップします.

一番上の「アシストと音声入力」をタップ.

さらに, 一番上の「アシストアプリ」の表示されてる文字の部分(歯車ではない) を押します.

この画面では現在「Launcher3」というアプリがセットされていますので. 歯車を押すとそのアプリの設定画面が表示されます. ホームボタン長押しから利用するアプリ自体を変更する場合は文字の部分をタップします.

インストールしているアプリのうち, アシスト機能に対応しているアプリの一覧が表示されますので, 利用したいアプリをタップしてセットします.

例えば「なし」を選択すると, ホームボタンを長押ししたときの反応はなくなります.

正直, 最近のOSまわりの設定は複雑すぎじゃない?

ホームボタン長押しアプリの変更や削除する方法

【3年連続3回目】ホームボタン長押し から起動されるアプリの変更は「アシス」で