LoginSignup
0
1

More than 5 years have passed since last update.

Boccoのメッセージを再生するトリガをサーバに置く

Last updated at Posted at 2019-02-05

気がつけば MyThings のサービスが1/31で終了とのこと。
Boccoをちょっとしたお知らせに使用していたのですが、でくの坊と化しました・・
が、お知らせを発行する機能は便利だったので、別の方法を探します。

IFTTTというアプリが便利そうです。が、Boccoに対応していません。でも他に手がなさそうなので、IFTTT使います。面倒だけどAPI使って実行するしかない(のかなあ)。

どうにか動いたので、手順含め公開しておきます。なお、外からアクセスできるHTTPサーバを持っていないとこの方法は実現できません。

API KEY の入手

Boccoを売っているユカイ工学さんに申し込みます。数日後にメールでAPI KEYが伝えられます。翌日ぐらいに来た記憶。ありがとうございます。

Room ID の入手

単に喋らせるだけの場合、Roomは固定で良いわけです。API KEY の使用方法サンプルに、Room IDの取得方法が掲載されているのでその通りにコンソールから実行。
access tokenは毎回変わるので都度取得が必要ですが、Room IDは不変なので、あらかじめ調べておいてプログラムに埋め込みます。

サーバにプログラムを配備

以下のphpプログラムを配備します。使用する際は
・APIKEY(Sbalsdkflaksevd8fajsefxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
・アカウント(your_account@mailaddr.com)
・パスワード(your_password)
・Room ID(11111111-2222-333f-your-roomkeyxxxxx)
を置換してください。もちろんメッセージの'ヤッホー'も。

それから、アカウントとパスワードは捨てアカ作りました。平文パスワードとか置きたくないですからね。

call.php
<html>
  <head>
    <meta charset="UTF-8">
    <title>bocco</title>
  </head>
<body>
<?php
exec_bocco();
?>
</body>
</html>
<?php
function exec_bocco() 
{
    send_message('ヤッホー');
}   

function send_message($message)
{
    $url = "https://api.bocco.me/alpha/sessions";
    $post_data = array("apikey" => "Sbalsdkflaksevd8fajsefxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                       "email" => "your_account@mailaddr.com",
                       "password" => "your_password");
    $dat = getApiDataCurl($url, $post_data, NULL);
    if ($dat == []) {
        echo "<h1>no dat!</h1>\n";
        return;
    }
    $token = $dat{'access_token'};

    $url = "https://api.bocco.me/alpha/rooms/11111111-2222-333f-your-roomkeyxxxxx/messages";

    $post_data = array("access_token" => $token,
                       "unique_id" => uuid(),
                       "media" => "text",
                       "text" => $message);
    $header_data = array("Accept-Language: ja");

    $dat = getApiDataCurl($url, $post_data, $header_data);

    echo "<p>".date("Y/m/d H:i:s")."</p>\n";
    echo "<p>".$message."</p>\n";
}

function getApiDataCurl($url, $post_data, $header_data)
{
    $option = [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 5,
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt_array($ch, $option);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    if ($header_data != NULL) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);
    }

    $json    = curl_exec($ch);
    $info    = curl_getinfo($ch);
    $errorNo = curl_errno($ch);

    curl_close($ch);

    if ($errorNo !== CURLE_OK) {
        echo "<h1>errorNo:".$errorNo."</h1>\n";
        return [];
    }

    if ($info['http_code'] !== 200 && $info['http_code'] !== 201) {
        echo $url."\n";
        echo "<h1>http_code:".$info['http_code']."</h1>\n";
        echo "result:".$json."\n";
        return [];
    }

    $jsonArray = json_decode($json, true);
    return $jsonArray;
}

function uuid()
{
    $data = openssl_random_pseudo_bytes(16);
    #$data = random_bytes(16);
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40); 
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80); 
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

?>

なんだかんだで苦労しました。uuidの生成って、PHPにAPIないんですねえ。

IFTTTアプリのWebHookで呼び出す

サーバに置いた上記のプログラムを呼び出すように設定して、完成です。
テストする限り、動いている模様。めでたしめでたし。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1