ActiveNotifier
Notify message through webhooks.
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
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.
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 locals data, like the familiar ActiveView does.
ActiveNotifier.configure do |config|
config.channel_tokens = { my_channel: "xxx" }
config.template_home = Rails.root.join("app", "views", "active_notifier")
end
echo "## #{data[:title]\n> #{data[:body]}}" > app/views/active_notifier/my_channel.markdown.erb
ActiveNotifier.exec(token_channel: :my_channel, template: :my_channel, data: { title: "Message Title", body: "Message Body" })
Also when token_channel and template 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" })
Advanced 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.