DIDComm 信件反应协议的 Credo 扩展模块
概览
这个 信件反应 扩展模块 克里多语 使代理人能够管理 反应协议,允许用户发送和接收对代理之间消息的emoji反应.
安装
npm install @2060.io/credo-ts-didcomm-reactions
使用量
将模块添加到您的代理
要使用反应模块,请添加到您的代理模块配置中:
import { DidCommReactionsModule } from '@2060.io/credo-ts-didcomm-reactions'
const agent = new Agent({
modules: {
// ...other modules
reactions: new DidCommReactionsModule(),
},
})
发送信件反应
发送信息反应(如emoji反应或去除反应):
const didcommReactions = [{ messageId: associatedMessageId ?? '', action: DidCommMessageReactionAction.React, emoji }]
// To remove a previous reaction:
didcommReactions.push({
messageId: associatedMessageId ?? '',
action: DidCommMessageReactionAction.Unreact,
emoji: myPreviousReaction.emoji,
})
await agent.modules.reactions.send({
connectionId: connection.id,
reactions: didcommReactions,
})
您也可以针对动作发送反应 :
if (action.type === AgentActionType.SendReaction) {
const parameters = action.parameters as {
didcommConnectionId: string
didcommReactions: {
messageId: string
action: DidCommMessageReactionAction
emoji: string
}[]
}
const { didcommConnectionId, didcommReactions } = parameters
return async (options: { agent: MobileAgent }) => {
await options.agent.modules.reactions.send({
connectionId: didcommConnectionId,
reactions: didcommReactions,
})
return { outgoingMessageType: DidCommMessageReactionsMessage.type.messageTypeUri }
}
}
处理反应事件
订阅反应事件以更新您的UI或消息存储, 当收到反应时 :
import { MessageReactionsReceivedEvent } from '@2060.io/credo-ts-didcomm-reactions'
const messageReactionsReceivedListener = async (data: MessageReactionsReceivedEvent) => {
const reactions = data.payload.reactions
// Example: update message reactions in your store
for (const reaction of reactions) {
const relatedEntries = findAllByAssociatedMessageId(realm, reaction.messageId)
for (const entry of relatedEntries) {
const entryReactions = entry.reactions ? entry.reactions : []
const reactionIndex = entryReactions.findIndex(
(item) => item.role === ChatEntryRole.Receiver && item.emoji === reaction.emoji,
)
if (reaction.action === 'react' && reactionIndex === -1) {
entryReactions.push({ emoji: reaction.emoji, role: ChatEntryRole.Receiver })
}
if (reaction.action === 'unreact' && reactionIndex !== -1) {
entryReactions.splice(reactionIndex, 1)
}
entry.reactions = entryReactions
entry.updatedAt = new Date().getTime()
}
}
}
// Subscribe to the event
agent.events.on('DidCommMessageReactionsReceived', messageReactionsReceivedListener)
特征
- 发送信件反应:以emojis对消息作出反应或去除反应.
- 事件订阅:倾听反应事件以更新UI或触发工作流程.
- 协议整合:与Credo代理和DIDComm协议的无缝集成.
API 参考
见 源代码 关于现有类别和方法的详细信息:
DidCommReactionsApi: 主API用于发送反应.DidCommReactionsService: 反应逻辑的内部服务.DidCommReactionsModule: Credo 代理的模块集成.
许可证
Apache 2.0 (英语)
更多信息,请参见 克里多文档。 。 。 .