メンション機能 #
はじめに #
daab からメンション付きのメッセージを送信する方法と、daab が受信したメッセージからメンション情報を取得する方法について説明します。
メンション付きメッセージの送信 #
markup 関数を使ってテキストにメンションを埋め込むことができます。引数として daab のユーザーオブジェクトを渡してください。
robot.hear(/message with mention$/, (res) => {
const mention = robot.direct.mention;
const at = mention.markup;
const user = robot.brain.userForName('徳島 太郎');
res.send(`${at(user)} こんにちは!`);
});トークに参加しているすべてのユーザーにメンションを付けたい場合は forAll で取得される特殊値を渡します。
robot.hear(/message with mention$/, (res) => {
const mention = robot.direct.mention;
const at = mention.markup;
const ALL = mention.forAll;
res.send(`${at(ALL)} 全員に連絡です。`);
});アクションスタンプについても上記と同様にメンションを付けられます。ただし、セレクトスタンプの選択肢にはメンションを付けられません。
受信したメッセージからメンション情報を取得 #
res.message からメンション情報を取得できます。
robot.hear(/check mention$/i, (res) => {
const all = res.message.mentionAll;
const me = res.message.mentionMe;
const mentions = res.message.mentions;
// メンションの ID 情報を使ってユーザーを特定する
const users = mentions.map((m) => robot.brain.userForId(m.user.id));
// ...
});各フィールドの仕様は以下の通りです。
| フィールド | 型 | 説明 |
|---|---|---|
mentionAll | boolean | メッセージのメンションが @ALL の場合は true、それ以外は false |
mentionMe | boolean | メッセージのメンションに自分が含まれる、または @ALL の場合は true、それ以外は false |
mentions | Mention[] | メッセージのメンション情報の配列、@ALL の場合は空配列 |
Mention オブジェクトは以下のような構造になっています。
{
user: {
id: '_287445236_2112454933' // メンションされたユーザーの ID
}
}各フィールドがどのような値になるのかをパターン別に示すと以下のようになります。
@ALLが付けられたメッセージを受信した場合mentionAll === truementionMe === truementions.length === 0
- 自分宛のメンションが付けられたメッセージを受信した場合
mentionAll === falsementionMe === truementions.length > 0
- 自分以外のユーザー宛にメンションが付けられたメッセージを受信した場合
mentionAll === falsementionMe === falsementions.length > 0