LoginSignup
6
1

More than 3 years have passed since last update.

お仕事の中にある『T』を僕たちと一緒に探していこう!!!

Last updated at Posted at 2020-02-13

ティー!ティティー!ティー!ティー!ティティー!

お仕事に疲れたみんな!僕たちと一緒にお仕事の中にある『T』を探しにいこう!

ティーーーーー!!!!!

投稿した文字列の中から『ティ』が含まれる言葉を見つけてスレッドに通知を飛ばします!
(『ティ』の文字が少なかったので『チ』も含めてます)
ezgif.com-video-to-gif (1).gif

一緒に『T』を探す愉快な仲間たち

Slack API
COTOHA API Portal
GoogleCloudFunctions (定期実行出来る環境であれば何でもOK)
・ Python 3.7

作り方

SlackBotを用意する

Slack API からアプリを作っていきます。
まずは『T』を探すSlackBot(TT兄弟Bot)を用意しましょう。
create_slackobot.jpg
ところでSlackBotのUIが変わったのご存知ですか?
APP_HOMEに名前の登録をしないとBotとして利用ができないのでお忘れなく!
apphome.jpg
必要なScopeは以下の通りです

Scope 使用するmethod 目的
channels:history conversation.history 投稿を取得する
chat:write conversations.replies 投稿にコメントを残す

COTOHA API を用意する

COTOHA API Portal からアカウントを作成しましょう。
作成すると、以下のようなログインページを見れるようになると思います。
cotoha-loginpage.jpg
これらのパラメータを使ってCOTOHA APIを使っていきます。

COTOHA APIを試す

アクセストークンの作り方
curl -X POST \
 -H "Content-Type:application/json" \
 -d '{
  "grantType": "client_credentials",
  "clientId": "CLIENT_ID",
  "clientSecret": "CLIENT_SECRET"
}' https://api.ce-cotoha.com/v1/oauth/accesstokens

こちらを実行すると

{
  "access_token": "ACCESS_TOKEN",  // アクセストークン
  "token_type": "bearer",
  "expires_in": "86399" , // 24時間有効
  "scope": "" ,
  "issued_at": "1581398461378"
}

アクセストークンを取得できます。

APIの実行

今回は構文解析のAPIを使いたいので 構文解析 リファレンス を参考に実行します。

curl -X POST \
 -H "Content-Type:application/json;charset=UTF-8" \
 -H "Authorization:Bearer ACCESS_TOKEN" \
 -d '{
  "sentence":"犬は歩く。",
  "type": "default"
}' https://api.ce-cotoha.com/api/dev/nlp/v1/parse


実行結果
{
  "result" : [ {
    "chunk_info" : {
      "id" : 0,
      "head" : 1,
      "dep" : "D",
      "chunk_head" : 0,
      "chunk_func" : 1,
      "links" : [ ]
    },
    "tokens" : [ {
      "id" : 0,
      "form" : "犬",
      "kana" : "イヌ",
      "lemma" : "犬",
      "pos" : "名詞",
      "features" : [ ],
      "dependency_labels" : [ {
        "token_id" : 1,
        "label" : "case"
      } ],
      "attributes" : { }
    }, {
      "id" : 1,
      "form" : "は",
      "kana" : "ハ",
      "lemma" : "は",
      "pos" : "連用助詞",
      "features" : [ ],
      "attributes" : { }
    } ]
  }, {
    "chunk_info" : {
      "id" : 1,
      "head" : -1,
      "dep" : "O",
      "chunk_head" : 0,
      "chunk_func" : 1,
      "links" : [ {
        "link" : 0,
        "label" : "agent"
      } ],
      "predicate" : [ ]
    },
    "tokens" : [ {
      "id" : 2,
      "form" : "歩",
      "kana" : "アル",
      "lemma" : "歩く",
      "pos" : "動詞語幹",
      "features" : [ "K" ],
      "dependency_labels" : [ {
        "token_id" : 0,
        "label" : "nsubj"
      }, {
        "token_id" : 3,
        "label" : "aux"
      }, {
        "token_id" : 4,
        "label" : "punct"
      } ],
      "attributes" : { }
    }, {
      "id" : 3,
      "form" : "く",
      "kana" : "ク",
      "lemma" : "く",
      "pos" : "動詞接尾辞",
      "features" : [ "終止" ],
      "attributes" : { }
    }, {
      "id" : 4,
      "form" : "。",
      "kana" : "",
      "lemma" : "。",
      "pos" : "句点",
      "features" : [ ],
      "attributes" : { }
    } ]
  } ],
  "status" : 0,
  "message" : ""
}


簡単に文字のカナが取れました!
これを使って、『T(ティー)』を探します!!

コードを書いていく

今回作る機能の構成は以下の図のようにしていきます。
TTflow.jpg
コードは GitHub に公開しているので自由に御覧ください。

コードを外部公開する

無料で試せる環境を探していたのですが、以下の記事が参考になりました。
Google Cloud Function + Cloud Scheduler + Python で定期的に Twitter 投稿する
今回 GitHub に置いているコードはトークンを書き換えるだけでコピペで行けるはず…

注意点

  • COTOHA APIの各APIの実行回数は 1000回/日 まで
  • SlackBotは該当のチャンネルに追加する必要があります

まとめ

今回 COTOHA API を使ってみましたが、英語 → カナ が出来るのが衝撃で、ノリで『T』を探すツールを作ってみました!
『T』以外にも検知する文字列を自由に変えることが出来るので、『あつい』 と言ったら負けなチャンネルを作ることや『できない』と言ったら励ましてくれるチャンネルなんかも作れると思います。
色々作ってみてはいかがでしょうか?ここまで読んで頂きありがとうございました!

6
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
6
1