node.jskik

How to send a message to all subscribed users with kik bot


I'm just a beginner trying to learn how to write a bot for kik. I'm trying to write it with the node js framework that kik has provided.

I want to send a message to all subscribed users of the bot; I found this in their docs:

bot.send(Bot.Message.text('Hey, nice to meet you!'), 'a.username');

but I'm confused as to how they get the username of the subscribed user. I tried using bot.getUserProfile.username, but it seems to be undefined.

Thanks for any help! Also, any tips on how this bot works would be appreciated! I have no web development experience; why does this bot have to be on a server?


Solution

  • First of all, if you want to send a blast to all of your users, I'd recommend using the broadcast API, which allows you to send messages by batches of 100 (instead of 25 for the regular send() API). You can use the broadcast API like this:

    bot.broadcast(Bot.Message.text('some message'), ['username1', 'username2']);
    

    Documentation for this method can be found here.

    As for sending messages to all of your users, you will need to have a list of usernames somewhere (in a database, for example). At the moment, Kik does not provide a way to get your list of subscribers.

    Something like this would work:

    bot.use((msg, next) => {
        let username = msg.from; // Find the username from the incoming message
    
        registerInDatabase(username); // Save it somewhere
        next(); // Keep processing the message
    });
    

    You will need to make sure that this is placed before you declare any other handler (such as bot.onTextMessage(), for instance).