Skip to content

Listening for Events

Chris Watson edited this page Sep 9, 2019 · 5 revisions

Intro

Tourmaline provides event listeners which are fired every time one of several UpdateAction's is fired.

Event Listener Annotation

Like commands, it's possible to annotate a method with an On annotation to designate that method as an event listener.On accepts any of the UpdateAction's as an argument (although you can use symbols as well) and passes the full Update to the method every time that action is fired.

As an example, let's listen for text messages and print them to the console:

@[On(:text)]
def text_listener(update)
  if message = update.message
    logger.debug("Text: #{message.text}")
  end
end

Non-annotation Method

As with commands you can also add event listeners without using an annotation.

bot.on(:text) do |update|
  if message = update.message
    bot.logger.debug("Text: #{message.text}")
  end
end
Clone this wiki locally