我目前正在尝试使用SendBird SDK,并且一直在尝试调用此函数并在执行操作时出现错误。下面是他们在其文档中提供的方法。
channel.sendUserMessage(message, data, customType, function(message, error){
if (error) {
console.error(error);
return;
}
console.log(message);
});
这是我使用的:
channelList[i].sendUserMessage(message, function (message, error) {
if (error) {
console.error(error);
return;
}
console.log(message);
document.getElementById(url + "ChatInput").value = "";
});
我回来的错误是:
I {name: "SendBirdException", code: 900025, message: "Message is blocked due to invalid type of 'mentioned_user_ids'"}
code: 900025
message: "Message is blocked due to invalid type of 'mentioned_user_ids'"
name: "SendBirdException"
现在我只上传一个字符串,一开始确实很困惑,其中提到提到的用户名来自哪里,但我知道了原因。我相信在SendBird SDK中,他们基于此文件使用Typescript和针对不同参数的多个重载:
https://github.com/smilefam/SendBird-SDK-JavaScript/blob/master/SendBird.d.ts
,尤其是该文件中的这一部分:
sendUserMessage(userMessageParams: UserMessageParams, callback: messageCallback): UserMessage;
sendUserMessage(message: string, callback: messageCallback): UserMessage;
sendUserMessage(message: string, data: string, callback: messageCallback): UserMessage; // DEPRECATED
sendUserMessage(message: string, data: string, customType: string, callback: messageCallback): UserMessage; // DEPRECATED
sendUserMessage(
message: string,
data: string,
customType: string,
targetLanguages: Array<string>,
callback: messageCallback
): UserMessage;
我相信,因为我没有使用Typescript,所以我相信它默认为该列表中使用userMessageParams的第一个函数,该函数包括:
interface UserMessageParams {
message: string;
data: string;
customType: string;
targetLanguages: Array<string>;
mentionType: 'users' | 'channel';
mentionedUserIds: Array<string>;
mentionedUsers: Array<User>;
pushNotificationDeliveryOption: 'default' | 'suppress';
}
其中包含之前在错误中提到的属性为“提及的用户ID”。
我的问题是,是否可以将其默认为仅包含字符串和回调函数的简单重载:
sendUserMessage(message: string, callback: messageCallback): UserMessage;
代替:
sendUserMessage(userMessageParams: UserMessageParams, callback: messageCallback): UserMessage;
不使用Typescript。
我尝试通过以下方式自己制作对象:
var userMessageParams = new sb.UserMessageParams();
userMessageParams.message = message;
userMessageParams.mentionType = "channel";
channelList[i].sendUserMessage(userMessageParams, function (message, error) {
if (error) {
console.error(error);
return;
}
console.log(message);
document.getElementById(url + "ChatInput").value = "";
});
但是我仍然收到与上述相同的错误。
null_data:null
_mentionType:"channel"
_mentionedIds:["d155d9e51f3e4897988e39a8f33c89a6"]
_mentionedUserIds:["d155d9e51f3e4897988e39a8f33c89a6"]
_mentionedUsers:[]
_message:"message"
_pushNotificationDeliveryOption:null
_targetLanguages:null
我手动在userMessageParams中设置属性的值:
userMessageParams.mentionedUserIds = [ channelList[0].members[1].userId, channelList[0].members[0].userId];
_customType:null
_data:null
_mentionType:"channel"
_mentionedIds:["d155d9e51f3e4897988e39a8f33c89a6"]
_mentionedUserIds:["d155d9e51f3e4897988e39a8f33c89a6"]
_mentionedUsers:[]
_message:"message"
_pushNotificationDeliveryOption:null
_targetLanguages:null
//Inside console in Chrome
channelList[0].members[1].userId
"d155d9e51f3e4897988e39a8f33c89a6"
channelList[0].members[0].userId
"1924c64299284da6b8e6366a7f0b7a7c"
(我不知道为什么它只在userIds中添加其中之一,我在添加之前和之后都在控制台中验证了它们的值。如果它与背景中的senderId相同,则可能不会添加值,或者那是我最好的猜测) 而且我仍然遇到相同的错误。
我不想提及某些用户,因为它仅用于一对一聊天,因此我并不需要它。
我现在看到的两个可能的解决方案是:
很抱歉,如果有人有任何信息可以帮助您或者需要进一步澄清以帮助我,谢谢,一切将不胜感激!
...如果有人疯狂地看着它(或我认为是位于SendBird.min.js中的东西):
this.sendUserMessage = function(e, n, t, r, i) {
var a = this
, s = le.MentionType.USERS
, o = []
, l = ue.PushNotificationDeliveryOption.DEFAULT;
if ("function" == typeof n && (i = n,
n = "",
t = "",
r = [],
e instanceof ue)) {
var u = e;
e = u.message,
n = u.data,
t = u.customType,
r = u.targetLanguages,
s = u._mentionType,
o = u._mentionedIds,
l = u.pushNotificationDeliveryOption ? u.pushNotificationDeliveryOption : ue.PushNotificationDeliveryOption.DEFAULT
}
if ("function" == typeof t && (i = t,
t = "",
r = []),
"function" == typeof r && (i = r,
r = []),
r || (r = []),
"string" == typeof r && (r = [r]),
null === n && (n = ""),
null === t && (t = ""),
"string" != typeof e || "string" != typeof n || "string" != typeof t || !Array.isArray(r) || [ue.PushNotificationDeliveryOption.DEFAULT, ue.PushNotificationDeliveryOption.SUPPRESS].indexOf(l) < 0)
return void U(null, new I("Invalid parameter.",C.INVALID_PARAMETER), i);
var c = null;
if (!he.getInstance() || !he.getInstance().currentUser)
return i && U(null, new I("Connection should be made first.",C.CONNECTION_REQUIRED), i),
c;
var d = q.bMessage(this.url, e, n, t, s, o, r, l)
, h = M.build(d.requestId, 0, he.getInstance().currentUser, this, e, n, t, r, (new Date).getTime(), null, s, null, o);
return c = new M(h),
he.getInstance().sendCommand(d, function(e, n) {
if (n)
return void U(null, new I(n.message,n.code), i);
var t = new M(e.getJsonElement())
, r = he.getInstance().currentUser;
r && t._sender && r.userId === t._sender.userId && (r.nickname !== t._sender.nickname && (r.nickname = t._sender.nickname),
r.profileUrl !== t._sender.profileUrl && (r.profileUrl = t._sender.profileUrl)),
a.isGroupChannel() && F.getChannel(a.url, function(e, n) {
if (R) {
var r = e;
e = n,
n = r
}
if (n)
return void U(null, n, i);
e.lastMessage = t;
for (var a in he.getInstance().channelHandlers) {
he.getInstance().channelHandlers[a].onChannelChanged(e)
}
}),
U(t, null, i)
}),
c
}
答案 0 :(得分:0)
SendBird不使用TypeScript。接口文件就是这样写的。 https://github.com/smilefam/SendBird-SDK-JavaScript/blob/master/SendBird.d.ts 里面是香草JS。
您上面提到的代码无需定义 UserMessageParam 即可正常工作。
channelList[i].sendUserMessage(message, function (message, error) {
if (error) {
console.error(error);
return;
}
console.log(message);
document.getElementById(url + "ChatInput").value = "";
});
请尝试调试单步调试。另外,请检查具有类似实现的示例代码。 https://github.com/smilefam/SendBird-JavaScript