The MVC framework for chat apps built on a platform designed for communication systems.
THIS IS A WORK IN PROGRESS AND NOT READY FOR ANYTHING REAL YET
Build chat bot applications for the major chat bot application platforms using familiar model-view-controller architecture patterns that developers have been using for decades.
Juvet is an application framework that includes everything you need to build a chat application for all the major messaging platforms including:
- Slack RTM
- Slack Events API
- Slack Incoming Webhook (coming soon)
- Amazon Alexa
- Facebook Messenger
- Twillio SMS
- Custom...
Juvet offers all the features you need to build a scalable and maintainable chat application, including
- API Wrappers
- Message Queuing
- Middleware and Plugins
- Conversation Support
- NLP Support
- more to come...
The ROADMAP describes major upcoming features within each release.
This repository is available for sponsorship via GitHub Sponsors at https://github.com/sponsors/jwright.
If you or your company will benefit from a well-maintained and easy to use chat application framework, please consider a sponsorship. Your sponsorship will help with this development.
Thank you for the support! 💓
Juvet requires Elixir ~> 1.15 and Erlang/OTP 25+.
Every change is tested in CI against two version pairs:
- the oldest supported pair (Elixir 1.15 / OTP 25), so the declared floor is always proven to work
- the latest stable pair (currently Elixir 1.19 / OTP 28), with
--warnings-as-errors, so consumers on newer Elixir never see compiler warnings from Juvet
Raising the Elixir floor is considered a breaking change; it will only happen in a new minor version and will be called out in the CHANGELOG.
- Add the Juvet dependencies to your
mix.exsfile
# mix.exs
def deps do
[{:juvet, "~> 0.0.1"}]
end
- Install the depedencies
mix deps.get
- Ensure Juvet is started before your application
# mix.exs
def application do
[extra_applications: [:juvet]]
end
When Juvet starts, the following is what that process tree should look like.
+------------------+ +-------------------+
| |-----| ViewStateRegistry |
+--| ViewStateManager | +-------------------+
| | | +---------------------+
| +------------------+-----| ViewStateSupervisor |
+---------------+ +--------------+--+ +----------------+ +---------------------+
| | | | | |
| Juvet |----| BotFactory |-----| Superintendent |
| (application) | | | | |
| | +--------------+--+ +----------------+
+---------------+ | +-------------------+
| | |
+--| FactorySupervisor |
| |
+-------------------+
| |
| |
+---------------+ +---------------+
| | | |
| BotSupervisor | | BotSupervisor |
| | | |
+---------------+ +---------------+
| |
| |
+-----+ +-----+
| Bot | | Bot |
+-----+ +-----+
- Juvet - Application that starts the
Juvet.BotFactorysupervisor - BotFactory - Supervisor that starts the
Juvet.SuperintendentandJuvet.ViewStateManagerprocesses. - ViewStateManager - Supervisor that can manage the storage of any arbitray piece of data for
a given set of keys. Starts the
Juvet.ViewStateRegistryand a dynamic supervisor forJuvet.ViewStateprocesses. - ViewStateRegistry - Server to act as a registry service to convert keys (as Tuples) into pids in order
to identify
Juvet.ViewStateprocesses. - Superintdendent - The brains of the operation. Process checks the validity of the
configuration and if it is configured correctly, it starts
the
Juvet.Endpointprocess and theJuvet.FactorySupervisor - FactorySupervisor - Supervisor over all of the
Juvet.BotSupervisorprocesses. - BotSupervisor - Supervisor over one
Juvet.Botprocess as well as any additional supporting processes (likeJuvet.Receivers.SlackRTMReceiver) - Bot - Receives messages from the chat providers. It is responsible for processing messages and generating responses
You need to tell Juvet what bot module should be created when a new connection is made. You can do that with the following configuration.
# config/config.exs
config :juvet,
bot: MyBot,
slack: [
actions_endpoint: "/slack/actions",
commands_endpoint: "/slack/commands",
events_endpoint: "/slack/events",
options_load_endpoint: "/slack/options"
]
Juvet encodes and decodes JSON with Jason and calls Slack with Req by default. Both can be swapped:
# config/config.exs
config :juvet, :json_library, Jason
config :juvet, :http_client, Juvet.HTTPClient.Req
:json_library takes any module with encode!/1, decode/2 and decode!/2. :http_client takes any module that implements the Juvet.HTTPClient behaviour.
Options for the default client are passed straight to Req. For example, to stub Slack in your tests with Req.Test:
# config/test.exs
config :juvet, Juvet.HTTPClient.Req, plug: {Req.Test, Juvet.HTTPClient}
The client application that is using Juvet can use the router from your client application. You just need to mount the Juvet.Plug.
The router can be mounted inside the Phoenix endpoint by just adding:
# lib/my_phoenix_app_web/endpoint.ex
defmodule MyPhoenixAppWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :my_phoenix_app
# ...
plug Juvet.Plug
end
Juvet handles the Slack OAuth v2 ("Add to Slack") flow. Configure the endpoints and your Slack app credentials:
config :juvet,
router: MyRouter,
slack: [
oauth_request_endpoint: "/auth/slack",
oauth_callback_endpoint: "/auth/slack/callback",
client_id: System.fetch_env!("SLACK_CLIENT_ID"),
client_secret: System.fetch_env!("SLACK_CLIENT_SECRET"),
redirect_uri: "https://example.com/auth/slack/callback",
scope: "chat:write,commands"
]Then route the results in your router:
platform :slack do
oauth("success", to: "my_app.slack#oauth_success")
oauth("error", to: "my_app.slack#oauth_error")
endVisiting the request endpoint redirects to Slack. The success action receives the token response as context.auth_response. The error action receives context.error.
Juvet sends a random state to Slack and stores it in a signed, HttpOnly cookie scoped to the callback endpoint. The callback checks that the state Slack sends back matches the cookie. If it doesn't, the request goes to the error action with context.error set to :invalid_state. This protects the flow against CSRF and login injection.
The cookie is signed with the conn's secret_key_base, which Phoenix endpoints already set, so a Phoenix app needs no extra configuration. Without a secret_key_base (or to use a separate secret), set state_secret: System.fetch_env!("SLACK_STATE_SECRET") in the slack configuration and generate it with mix phx.gen.secret or openssl rand -base64 48. Don't reuse a secret from your Slack app, such as the signing secret.
Once you have the bot access token for your team, you are ready to go.
Once you have a bot access token for your team, you can connect to Slack via:
{:ok, bot} = Juvet.create_bot("MyBot")
You can handle messages from Slack by overriding the handle_event/3 function on your bot. This function can use pattern matching in order to handle various events from Slack.
defmodule MyBot do
use Juvet.Bot
def handle_event(platform, %{type: "message"} = message, state) do
# Add your logic here on how to handle a message event
{:ok, state}
end
def handle_event(platform, %{type: "file_created"} = message, state) do
# Add your logic here on how to handle a file_created event
{:ok, state}
end
end
You can send messages back to Slack from your bot by overridding the send_message/3 function on your bot. The second argument (state) should contain an (id) key which will be used to send the message to the correct team.
defmodule MyBot do
use Juvet.Bot
def handle_event(platform, %{type: "message", text: "Hello"} = message, %{id: id, channel: channel} = state) do
send_message(platform, state, %{type: "message", channel: channel, message: "Right back at cha!"})
{:ok, state}
end
end
You can run the tasks with the standard mix command:
mix test
You can re-record the responses from Slack with the following mix command:
MIX_ENV=test mix record token:<slack token here> channel:<slack channel id here> text:"<Welcome from Juvet!>" ts:<valid message timestamp here> user:<slack user id here> users:<slack user id here>,<another slack user id here>
You can record the casettes for just one method by adding a method parameter above (i.e. method:chat.update) and it will just re-record that one method.
You can create a Slack token for any of your teams by visiting the OAuth & Permissions area in your Slack API Apps.
- Clone the repository
git clone https://github.com/juvet/juvet - Create a feature branch
git checkout -b my-awesome-feature - Codez!
- Commit your changes (small commits please)
- Push your new branch
git push origin my-awesome-feature - Create a pull request
hub pull-request -b juvet:main -h juvet:my-awesome-feature
Copyright (c) 2018, Jamie Wright.
Juvet source code is licensed under the MIT License.