-
Notifications
You must be signed in to change notification settings - Fork 0
Description
The goal is to create a simple API to kickstart a chat bot project with very few technical skills, but enough flexibility to create a full featured one.
So the basic idea (inspired by express) is to have:
- Adapters: Network application that will make the bridge between the protocol/service and the bot (sending requests to the bot and receiving response/actions)
- Middleware: Functions that will be in charge of taking actions (
response) from the context (request)
From this we can create a very simple bot framework but it can become messy and not that easy to implement. To abstract the complexity of creating middleware functions, we could add some functions to compose middleware easily. These function can be splitted in 2 categories, filters and actions, but we will add events (which is a filter) category to make it clearer.
- Filters taking only requests that respond to specific conditions
- Event taking only a certain type of requests
- Actions taking actions when passed filters
So basically using the 3 categories in a function middleware could look like:
bot.use((req, res, next) => {
if (req.event !== 'message') return // event
if (!req.message.match(/hello/)) return // filter
res.say(`Hello ${req.username}`) // action
next()
})With composing we could do:
bot.use(compose(
bot.on('message'), // event
bot.match(/hello/), // filter
bot.say('Hello {username}') // action
))And finally what looks the best to me for it's simplicity:
bot()
.on('message') // event
.match(/hello/) // filter
.say('Hello {username}') // actionSo this should have the same behavior than the first middleware function.
Here is a draft of what can be done with the API:
import fatbot, {irc} from 'fatbot' // bot and built-in adapter
import telegram from 'fatbot-telegram' // third-party adapter
var bot = fatbot() // Create a bot
bot.add(irc, { // Add an adapter
server: 'freenode',
username: 'fatbot',
channels: ['#fatbot', '#chatbot']
})
bot.add(telegram, { // Add a second adapter
key: KEYS.TELEGRAM
})
bot(function(req, res, next){ // Add custom middleware
bot.log(`Message of type ${req.type} received`)
next()
})
// Easy middleware creation
bot()
.channel('#fatbot') // Filter to specific thread/channel
.on('talk') // Listen to specific event
.match(/hello/) // Filter by regex
.say('Yo man.') // Action taken
bot()
.on('message')
.on('private') // Listen to several events
.match(/fuck|shit/)
.ban() // Events/Filters/Actions can be defined by adapter
bot()
.on('connect')
.self(true) // Filter only bot messages
.log('I am connected')
bot()
.on('connect')
.say('Hello {username}!') // Template tags bound to request object
bot()
.channel('#fatbot')
.use(FATBOT_MIDDLEWARE) // Filter middleware usage
bot()
.on('message')
.match(/hello/)
.say(req => `Hello ${req.name}!`) // Function can also be passed
.kick()
.say('Oops. I did it again.') // Chain actions in order
bot.connect() // Launch the bot