xvi-botdeprecated

Bot Handler

Usage no npm install needed!

<script type="module">
  import xviBot from 'https://cdn.skypack.dev/xvi-bot';
</script>

README

npm-xvi-bot

Bot Handler

Example

//Add package
const Bot = require('xvi-bot');
const Helper = require('xvi-helper');

//set the environment (used for log)
process.env.ENV = 'DEV';

//Create instance of the scraper
var instance = new Bot({
    userId: 1,
    msgOldMinuteLimit: 10,
    msgNewMinuteLimit: 1,
    processCb: async function (msg) {
        console.log('Processing message !');
        return {
            content: "Processed this message !"
        };
    },
    sendCb: async function (msg) {
        console.log('Sending message !');
        console.log(msg);
    }
});

//test function
async function test() {
    try {
        var helper = new Helper();
        await instance.hear([{
            date: helper.now(),
            content: 'This is a test.',
            authorId: 2,
        }, {
            date: helper.now(),
            content: 'This is from me.',
            authorId: 1,
        }]);
    } catch (err) {
        console.log(err);
    }
}

test();

Will output:

Benoits-MacBook-Pro:npm-xvi-bot xvi$ node test.js
170918-145720 |   DEV.debug.Bot | canStart = true
170918-145720 |   DEV.debug.Bot | heard: 2 messages
170918-145720 |   DEV.debug.Bot | vet: {"date":"2017-09-18T06:57:20.398Z","content":"This is a test.","authorId":2}
170918-145720 |   DEV.debug.Bot | validate: {"date":"2017-09-18T06:57:20.398Z","content":"This is a test.","authorId":2}
170918-145720 |   DEV.debug.Bot | validate: OK
170918-145720 |   DEV.debug.Bot | notFromMe: OK
170918-145720 |   DEV.debug.Bot | hashMsg: {"date":"2017-09-18T06:57:20.398Z","content":"This is a test.","authorId":2}
170918-145720 |   DEV.debug.Bot | alreadyProcessed: No
170918-145720 |   DEV.debug.Bot | isRecentEnough: 2017-09-18 14:57:20 vs 2017-09-18 14:57:20
170918-145720 |   DEV.debug.Bot | isRecentEnough: Yes
170918-145720 |   DEV.debug.Bot | read: {"date":"2017-09-18T06:57:20.398Z","content":"This is a test.","authorId":2}
170918-145720 |   DEV.debug.Bot | hashMsg: {"date":"2017-09-18T06:57:20.398Z","content":"This is a test.","authorId":2}
170918-145720 |   DEV.debug.Bot | process "This is a test."
Processing message !
170918-145720 |   DEV.debug.Bot | vet: {"date":"2017-09-18T06:57:20.398Z","content":"This is from me.","authorId":1}
170918-145720 |   DEV.debug.Bot | validate: {"date":"2017-09-18T06:57:20.398Z","content":"This is from me.","authorId":1}
170918-145720 |   DEV.debug.Bot | validate: OK
170918-145720 |   DEV.debug.Bot | notFromMe: KO
170918-145720 |   DEV.debug.Bot | send
170918-145720 |   DEV.debug.Bot | validate: {"content":"Processed this message !","date":"2017-09-18 14:57:20","authorId":1}
170918-145720 |   DEV.debug.Bot | validate: OK
170918-145720 |   DEV.debug.Bot | hashMsg: {"content":"Processed this message !","date":"2017-09-18 14:57:20","authorId":1}
Sending message !
{ content: 'Processed this message !',
  date: '2017-09-18 14:57:20',
  authorId: 1,
  _hash: '619fc177734b33a7caeb819a88922ad7' }