Skip to content

Bot API Tutorial (part 3): Creating your first bot

_iPhoenix_ edited this page Jan 31, 2018 · 1 revision

UniChat bots have very simple syntax.

To get started, we recommend understanding the following example. The code is also located in /js/bots.js, which is where you put your other bots. (of course, you would delete this code and replace it with your bots. This is just an example to help you understand the concept).

We highly recommend the text editor Sublime Text.

//Create a bot, "testBot", that gets called whenever a message starts with "!"
var testBot = new Bot("testBot", "!");

//When UniChat is ready to load bots, register the bot(s). If you are doing multiple bots, you should put the bot.register() commands in the same initializeBots block. Syntax: bot.register().
function initializeBots() {
  testBot.register();
}

//this is the function called whenever a message starts with the command header. 
testBot.executeCommand =  function(data) {
  //This code initializes the variables:
  var poster = data.poster;
  var message = data.message;
  var timestamp = data.timestamp;
  var raw_timestamp = data.rawTimestamp;
  //if the first 4 characters of the message (starting from 0), minus the command header, are "ping", then highlight the user and say, "Pong!".
  if (message.substring(0,4) == "ping") {
  	testBot.respond(poster + ": Pong!");
  }
}

You can test out this bot by launching index.html from the download. If you make any other bots in the /js/bots.js file, then you can test them out by launching the same file.

This code creates a bot called testBot, which is called whenever a message starts with ! that responds Pong! whenever a user types !ping.

Note that all commands are made lowercase when they are called, so !ping is the same as !PiNg, which is the same as !piNG, which is the same as !Ping.

An example of this demo running correctly:

[02:15:51] user_de83fe: !ping
[02:15:51] [testBot]: user_de83fe: Pong!

Note that, for bots to work on the full version of UniChat, they have to be added to the admin console, which is currently something only _iPhoenix_ can do.

If your bot is done, then you can submit your bot's code to us, and we will implement it. We recommend sending us the bot's code somehow. A good option is to create a Github repo for the bot and send us a link to the repo by highlighting the "user" bots and pasting the link.

We will add your bot as soon as possible! (if it is good enough)