0
1

More than 3 years have passed since last update.

【Noby API】ノビィ取り扱い説明書 コマンド機能編【CotoGoto】

Last updated at Posted at 2019-09-23

こんにちは。この記事は
【Noby API】ノビィ取り扱い説明書【CotoGoto】
【Noby API】ノビィ取り扱い説明書 使い方編【CotoGoto】
の続きの記事になります。

今回はコマンド機能についてを説明していきます。

1. コマンド機能について

コマンド機能は文章中に設定した単語が登場した場合、登録したコマンドIDを返却します。
NOBY_API_取扱い説明書_2019.png

2. コマンド機能を使って言葉とプログラムを連動する

こちらの説明は、使い方編のプログラムの拡張になります。
最初にコマンド機能の設定からコマンドの登録を以下のように登録します。

FireShot Capture 050 - Noby API ・ CotoGoto(コトゴト) - app.cotogoto.ai.png

2-1. プログラムの追加

プログラム以下のfunctionを追加します。

index.html
<script>
function makeSpotsDice(){
    var spotsmax = 6;//最大値
    var spotsmin = 1;//最小値
    var spotsdice = Math.random()*(spotsmax - spotsmin + 1) + spotsmin;
    spotsdice = Math.floor(spotsdice); //少数以下を丸める
    return spotsdice;
}


</script>

2-2. 前回プログラムを修正

前回のプログラムを以下のように修正します。

index.html
<script>
function send() {
    $.get('https://app.cotogoto.ai/webapi/noby.json',
      {
        appkey: 'YOUR_API_KEY',
        text: $('#message').val()
      },
      function(data) {
       console.log(data);
        if ('dice' == data.commandId) {
          $('#reply').html(makeSpotsDice() + 'の目が出たよ。');
        } else {
          $('#reply').html(data.text);
        } 
    );
}
</script>

3. 最後に

チャットをしているときに「サイコロを振って」と会話するとサイコロの目を返してくれると思います。
このように他の機能を作成したいときに参考にしてください。

※ソースについてはGithubに上げてあります。

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