Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update how bots register handlers #17

Open
byrnereese opened this issue May 26, 2022 · 0 comments
Open

Update how bots register handlers #17

byrnereese opened this issue May 26, 2022 · 0 comments

Comments

@byrnereese
Copy link

I would like to find a simpler way for bots to register handlers for different events. Right now, a bot consists mainly of a generic event handler. For example:

const botHandler = async event => {
    console.log(`Received ${event.type} event`)
    switch (event.type) {
    case 'Message4Bot':
        await handleBotMessage(event)
        break
    case 'Message4Others':
        await handleMessage(event)
        break
    default:
        console.log('Unknown event type: ' + event.type)
        break
    }
}

In your main index.js file you then register this handler like so:

const app = express();
var skills = []
var botOptions = {}
extendApp(app, skills, botHandler, botOptions);

Express registers handlers like so:

app.get('/zendesk/oauth', async (req, res) => {
    // do something
})

I would love it if bots could register event handlers in a similar fashion. The events I think bots can register handlers for are:

  • Processing any URLs/links found in a post
  • Processing commands transmitted to a bot, e.g. @botName command param
  • Processing interactive messaging events, e.g. submitting adaptive card forms
  • Processing messaging events, e.g. Message4Bot, PostAdded, BotDeleted, etc.

And I would love the registration of these handlers to be similar to express. Here is some pseudocode:

const app = express();
extendApp( app )
app.onUrl( function ( url, bot, response ) {
    // some logic
    response.postCard( adaptiveCard )
})
app.onCardSubmit( "action_name", function ( data, response ) {
    let variable = data.form_element_id
    // some logic
    response.postDialog( dialog )
})
app.onMessage4Bot( function ( event, response ) {
    response.postMessage( "hello" )
})
app.onBotCommand( 'command name', function ( args, response ) {
    let name = args[1]
    if (something_went_wrong) {
          throw BotError("this message will be displayed in a dialog")
    }
    response.postMessage( "hello " + name )
})

A few things I would like to point out:

  • The developer need only register callbacks/handlers for all the events we support.
  • Events handlers all receive a response object of some kind, and the developer uses this response object to perform common actions, e.g. posting messages/dialogs/cards as a response to the event/user
  • There is a consistent way to transmit errors and for those errors to be communicated to the user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant