Javascript/Typescript Overload Selection with SendBird

661 Views Asked by At

I am currently trying to use SendBird SDK and have been trying to call this function and getting errors back when I do. Below is there method they give on their documentation.

channel.sendUserMessage(message, data, customType, function(message, error){
    if (error) {
        console.error(error);
        return;
    }

    console.log(message);
});

Here is what I use:

channelList[i].sendUserMessage(message, function (message, error) {

    if (error) {
        console.error(error);
        return;
    }

    console.log(message);
    document.getElementById(url + "ChatInput").value = "";

});

The error I get back is this:

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"

Now I am only uploading a string and was really confused at first where mentioned_users_ids was coming from but I figured out why. I believe in the SendBird SDK they use Typescript and multiple overloads for different paramaters based on this file:

https://github.com/smilefam/SendBird-SDK-JavaScript/blob/master/SendBird.d.ts

and specifically this part in that file:

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;

I believe since I am not using Typescript I believe its defaulting to the first function in that list that uses userMessageParams which consists of:

interface UserMessageParams {
    message: string;
    data: string;
    customType: string;
    targetLanguages: Array<string>;
    mentionType: 'users' | 'channel';
    mentionedUserIds: Array<string>;
    mentionedUsers: Array<User>;
    pushNotificationDeliveryOption: 'default' | 'suppress';
}

Which includes the property that was mentions in the error before as 'mentioned_user_ids'.

My question is if is possible to default it to the simple overload of just the string and callback function:

sendUserMessage(message: string, callback: messageCallback): UserMessage;

instead of:

sendUserMessage(userMessageParams: UserMessageParams, callback: messageCallback): UserMessage;

without using Typescript.

What I tried

I tried to just make the object myself by doing:

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 = "";
});

But I still get the same error as mentioned above.

null_data:null
_mentionType:"channel"
_mentionedIds:["d155d9e51f3e4897988e39a8f33c89a6"]
_mentionedUserIds:["d155d9e51f3e4897988e39a8f33c89a6"]
_mentionedUsers:[]
_message:"message"
_pushNotificationDeliveryOption:null
_targetLanguages:null

I manually set the values of the property inside 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"

(I don't know why it only added one of them inside userIds, I verified both had values in the console before and after adding it. Probably doesn't add values if it's the same as the senderId in the background or that is the best guess I have) And I still get the same errors.

I don't want to mention certain users because this is just used for 1 on 1 chat so I don't really need it.

Solutions

The two possible solutions I see right now are:

  • Figure out of who default to the string overload (I have never used Typescript and don't know how its transcompiled to java-script so I don't know if that's possible).
  • Figure out where I am messing up on making the userMessageParams, and there I don't even want to mention any users.

Sorry for the essay, if anyone has any information that could help out or need more clarification to help me, anything would be greatly appreciated, thanks!

Transcompiled code

...if anyone is crazy enough to look at this, (or what I think it is) located in the 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
            }
1

There are 1 best solutions below

5
On

SendBird doesn't use TypeScript. The interface file is just written with that. https://github.com/smilefam/SendBird-SDK-JavaScript/blob/master/SendBird.d.ts Inside, it is vanilla JS.

The code that you mentioned above should work without needing to define the UserMessageParam.

    channelList[i].sendUserMessage(message, function (message, error) {

        if (error) {
            console.error(error);
            return;
        }

        console.log(message);
        document.getElementById(url + "ChatInput").value = "";

    });

Please try to debug stepping through. Also, please check sample code that has similar implementation. https://github.com/smilefam/SendBird-JavaScript