Repository
https://github.com/byteball/byteballcore
What Will I Learn?
Hello everyone. Today we're going to develop a simple Echo Bot for the Byteball Platform.
- You will learn how to receive messages
- You will learn how to send messages
Requirements
- Node.js installed
- A Wallet with Tingos.
Difficulty
Basic
Tutorial Contents
Set up
Let's first create our package.json file. You can do it either running npm init or manually.
{
"name": "test-echo-bot",
"version": "1.0.0",
"description": "Testing bot",
"main": "echo.js",
"author": "gustavoaca1997",
"license": "MIT",
"dependencies": {
"byteballcore": "git+https://github.com/byteball/byteballcore.git",
"headless-byteball": "git+https://github.com/byteball/headless-byteball.git"
}
}
I'm gonna explain the dependencies:
byteballcore: This is a library used in Byteball clients.headless-byteball: This is a headless alternative of the GUI wallet for Byteball network. It is designed for an always-online deployment on a server.
Now we're going to create the configuration file, called conf.js.
exports.deviceName = 'Echo bot';
exports.permanent_pairing_secret = '0000';
exports.hub = 'byteball.org/bb';
exports.bLight = true;
- Device name is an user friendly name of your device.
- The Pairing Secret is going to be part of your pairing code.
- The hub is set to default hub, operated by
.
Build bot
Let's create a file called echo.js. Inside of it, require the libraries needed:
const eventBus = require('byteballcore/event_bus.js');
const headlessWallet = require('headless-byteball');
const conf = require('byteballcore/conf.js');
Now all we gotta do is listen to text messages:
eventBus.on('text', function(from_address, text) {
// listen to messages
});
To send a message to the device that sent us the last message, we are gonna use sendMessageToDevice() from byteballcore/device.js
eventBus.on('text', function(from_address, text) {
var device = require('byteballcore/device.js');
device.sendMessageToDevice(from_address, 'text', text);
});
Bring bot to life
Now we need to install it dependencies with npm install and run it with node echo.js. We need to type a passphrase and it's all. You will se printed bot's pairing code (the code you will use to start chatting with it).