我喜欢新版的LINE消息API可以获取提及(mention)的信息!
我现在能够通过LINEBot获取提到我名字的信息了!
最近有更新来了。
消息API更新(2021年1月)
以前,无法获取群组聊天或对话室中@提及的信息。但通过使用消息Webhook事件对象中新添加的文本消息类型的属性,现在可以获取@提及的信息。
新添加的属性如下:
mention:包含@提及信息的对象。
mention.mentionees[]:一个或多个@提及。
mention.mentionees[].index:将文本的开头视为0,表示@提及在text属性中的起始位置。
mention.mentionees[].length:@提及的长度。例如,@example的长度为8。
mention.mentionees[].userId:被@提及的用户ID。详细信息,请参考「消息API参考」中的「文本」消息类型。
试一试
我用Node.js试试看。
我将使用官方SDK的Echo-Bot示例。
https://github.com/line/line-bot-sdk-nodejs/tree/next/examples/echo-bot
先看一下,event.message。
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
// ignore non-text-message event
return Promise.resolve(null);
}
+ console.log(event.message);
// create a echoing text message
const echo = { type: 'text', text: event.message.text };
// use reply API
return client.replyMessage(event.replyToken, echo);
}
macuser@macbook:~/line-bot-sdk-nodejs/examples/echo-bot (next *)$ node index.js
listening on 3000
{
type: 'text',
id: '13424246938488',
text: '@みかんBot @R @りんごBot @カワウソちゃん ',
mention: { mentionees: [ [Object], [Object], [Object], [Object] ] }
}
哇,提到的人们!让我们来看看更多。
function handleEvent(event) {
if (event.type !== 'message' || event.message.type !== 'text') {
// ignore non-text-message event
return Promise.resolve(null);
}
+ console.log(event.message.mention.mentionees);
// create a echoing text message
const echo = { type: 'text', text: event.message.text };
// use reply API
return client.replyMessage(event.replyToken, echo);
}
macuser@macbook:~/line-bot-sdk-nodejs/examples/echo-bot (next *)$ node index.js
listening on 3000
[
{ index: 0, length: 7, userId: 'Ud99df1c3b2066eca8348547af0c8be9f' },
{ index: 8, length: 2, userId: 'U89c0a22ed4a897463bd950519e0f72ba' },
{ index: 11, length: 7, userId: 'U054f7d1ddf16b75f1a8df5c39d043c9b' },
{ index: 19, length: 8, userId: 'U272633bd8ac62da518bac7b7469cc420' }
]
哦哦哦,太好了!
请稍等一下
不同意提供情报获取的用户的userId无法被获取。
macuser@macbook:~/line-bot-sdk-nodejs/examples/echo-bot (next *)$ node index.js
listening on 3000
[
{ index: 0, length: 7, userId: 'Ud99df1c3b2066eca8348547af0c8be9f' },
{ index: 8, length: 2, userId: 'U89c0a22ed4a897463bd950519e0f72ba' },
{ index: 11, length: 7, userId: 'U054f7d1ddf16b75f1a8df5c39d043c9b' },
{ index: 19, length: 16 }
]
请点击此处查看详细内容:
https://developers.line.biz/ja/docs/messaging-api/user-consent/
最后
获取提及信息可能会使范围大大扩大呢〜
很遗憾,机器人无法在群组或房间中提及成员。。。