Skip to content

Latest commit

 

History

History
81 lines (57 loc) · 1.88 KB

skills.md

File metadata and controls

81 lines (57 loc) · 1.88 KB

Skills

"Skill" is a mechanism to reuse chatbot code. If there is a chatbot feature which is common enough, you might want to build a skill.

A skill is just a plain JavaScript object with two optional properties:

  • handle
    • a function which will be invoked when there is an event
  • app
    • an express app

Code samples

Create a skill

const handle = async event => {
  const { type, text, group, bot } = event
  if (type === 'Message4Bot' && text === 'ping') {
    await bot.sendMessage(group.id, { text: 'pong' })
    return true // event handled
  }
  return false // event not handled
}
const app = express()
app.get('/hello', async (req, res) => {
  res.send('world')
})
const myCoolSkill = { handle, app }

myCoolSkill has the following behavior:

  • Whenever it receives "ping" from an user, it will reply with "pong"
  • It also create a new page at uri path '/hello', when visited it will display "world".

Use the skill

import createApp from 'ringcentral-chatbot/dist/apps'

const app = createApp(undefined, [
    myCoolSkill
])

app above is a chatbot app, and it has all the behaviors of myCoolSkill.

"catch-all" skill

You may need a catch-all skill

const handle = async (event, handled) => {
    if (!handled) {
      // This is an unhandled event
    } else {
      // event has been handled by other skills already
    }
    return true
}
const catchAllSkill = { handle }

Catch-all skill should be the last in the skills list

Real projects

Ping skill and bot

Google Drive skill and bot