ActiveNotifier

Notify message through webhooks.

Gem Version Build Status Test Coverage

Installation

Add this line to your application's Gemfile:

gem 'active_notifier'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_notifier

Support

Notifier adapter message type support options
Dingtalk text, markdown all

Basic Usage

Just notify the message through a webhook token, ActiveNotifier use Dingtalk by default, the webhook token need be a accessible dingtalk webhook token.

In your IRB environment:

ActiveNotifier.exec(token: "your-webhook-token", message: "your-message")

Is it too much trouble to set the token and message always? You can save commonly used ones and access them via better ways.

For token, ActiveNotifier use channel_tokens to store it, and for message, we can use the ERB template and data option for locals, like the familiar Rails does.

In any file you want to config ActiveNotifier:

ActiveNotifier.configure do |config|
  config.channel_tokens = { my_channel: "xxx" }
  config.template_home = SOME_DIR
end

Edit the template of my_channel:

$ echo "## #{data[:title]\n> #{data[:body]}}" > SOME_DIR/my_channel.markdown.erb

Then execute it in IRB:

ActiveNotifier.exec(token_channel: :my_channel, template: :my_channel, data: { title: "Message Title", body: "Message Body" })

Also when token_channel name and template name is the same, we can simplify it by using the first optional argument channel

ActiveNotifier.exec(:my_channel, data: { title: "Message Title", body: "Message Body" })

For Rails application

There are generators that simplify the above operations:

$ rails generate active_notifier:install

More knowledge

Send message with dynamic type

Message type will dynamic set according to a valid template, imagine we have two template files named order.text.erb and order.markdown.erb, now we want use the text type of template, we need add a type option

ActiveNotifier.exec(:order, data: { title: "Message Title", body: "Message Body" }, type: :text)

But if there is only one type of template, the type option is optional

# Now we have only template file `product.text.erb` for product channel

ActiveNotifier.exec(:product, data: { title: "Message Title", body: "Message Body" }) # Needn't type option

And also we can configure priority type for ActiveNotifier

ActiveNotifier.configure do |config|
  # default is :markdown
  config.priority_type = :text
end
# Now even we have template files `book.text.erb` and `book.markdown.erb` for book channel

ActiveNotifier.exec(:book, data: { title: "Message Title", body: "Message Body" }) # Needn't type option, and will choose the text type

Set a short constant

ActiveSupport hook after_initialize will set this by default

ActiveNotifer.config.const_name = :Notifier
Notifer.exec(...)

Help and Docs

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/pinewong/active_notifier. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Code of Conduct

Everyone interacting in the ActiveNotifier project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.