LINE Messaging APIでLINE botを作る(2) Habitica連携
の続き。
掃除を習慣化するため習慣化をゲーミフィケーションするHabiticaを活用する。Habiticaについては以下を参照。
Habiticaの良い点としてはAPIが充実しているところ。
メッセージに「掃除」や「片付け」といったキーワードを含む場合、Habiticaの掃除タスクのスコアを上げるといったことができるようになる。
タスクIDの確認
Habiticaで作成したタスクのスコアを変動させるにはタスクIDが必要となるため、GetUserTasks
から確認する。
https://habitica.com/apidoc/#api-Task-GetUserTasks
APIのリクエストにはリクエストヘッダにユーザーIDとAPIトークンを指定する必要があるためあらかじめ確認しておく。(webの場合、ユーザー → 設定 → APIで確認可能)
$ curl -H 'x-api-user: (ユーザーID)' -H 'x-api-key: (APIトークン)' https://habitica.com/api/v3/tasks/user
{ "success": true, "data": [ { "challenge": {}, "group": { "approval": { "required": false, "approved": false, "requested": false }, "assignedUsers": [], "sharedCompletion": "singleCompletion" }, "up": true, "down": false, "counterUp": 2, "counterDown": 0, "frequency": "weekly", "history": [ { "date": 1558947241919, "value": 1, "scoredUp": 1, "scoredDown": 0 }, { "date": 1559097092220, "value": 1.243614090235369, "scoredUp": 1, "scoredDown": 0 } ], "type": "habit", "notes": "部屋の乱れは心の乱れ、ゴミの重さは心の重さ。こまめに掃除して心身ともに清潔に保つ\n", "tags": [], "value": 0.6218070451176845, "priority": 1, "attribute": "str", "reminders": [], "createdAt": "2019-05-27T07:51:09.002Z", "updatedAt": "2019-05-29T22:26:58.534Z", "_id": "(タスクID)", "text": "部屋の掃除", "userId": "(ユーザーID)", "id": "(タスクID)" }, ~ 中略略 ~ ], "appVersion": "4.97.0" }
上記の_id
とid
がタスクIDとなる。
tidyMyRoom()の作成
タスクIDが分かればScoreTask
でスコアを増減することができる。
https://habitica.com/apidoc/#api-Task-ScoreTask
掃除タスクのスコアをアップさせる関数は以下の通り。
const habiticaUser = process.env.HABITICA_USER const habiticaKey = process.env.HABITICA_KEY const habiticaTask = process.env.HABITICA_TASK function tidyMyRoom () { let options = { url: `https://habitica.com/api/v3/tasks/${habiticaTask}/score/up`, headers: { 'x-api-user': habiticaUser, 'x-api-key': habiticaKey }, method: 'POST' } request(options, function (error, response, body) { if (error) { console.log(error) } console.log(body) }) }
echoman()の更新
メッセージに「掃除」や「片付け」といった単語が含まれている場合tidyMyRoom()
を実行するようにechoman()
を更新する。
async function echoman (ev) { // const pro = await client.getProfile(ev.source.userId) if (ev.message.text.includes('掃除') | ev.message.text.includes('片付け')) { tidyMyRoom() } let msg = await conversation(ev.message.text) return client.replyMessage(ev.replyToken, { type: 'text', text: msg }) }
我が家にはRATOC SYSTEMS スマート家電コントローラRS-WFIREX3があるので次は部屋の照明の消灯を行う。