View Source Telegex.Hook.GenHandler behaviour (Telegex v1.8.0)

Generate your webhook handler, which includes a supervisor with a Plug-based child.

Add plug and remote_ip to your application's deps because they are required for webhook mode.

You also need to configure adapters for hooks, which provide web services.

Based on Bandit - bandit

# add `bandit` to your dpes.
config :telegex, hook_adapter: Bandit

Based on Cowboy - plug_cowboy

# add `plug_cowboy` to your dpes.
config :telegex, hook_adapter: Cowboy

To work in webhook mode:

  1. Create a new module, like YourProject.HookHandler
  2. Use Telegex.Hook.GenHandler
  3. Implement on_boot/0 and on_update/1 callback functions
  4. Add your module to the supervision tree

Hook handler example:

defmodule YourProject.HookHandler do
  use Telegex.Hook.GenHandler

  @impl true
  def on_boot do
    # read some parameters from your env config
    env_config = Application.get_env(:your_project, __MODULE__)
    # delete the webhook and set it again
    {:ok, true} = Telegex.delete_webhook()
    # set the webhook (url is required)
    {:ok, true} = Telegex.set_webhook(env_config[:webhook_url])
    # specify port for web server
    # port has a default value of 4000, but it may change with library upgrades
    %Telegex.Hook.Config{server_port: env_config[:server_port]}
    # you must return the `Telegex.Hook.Config` struct ↑
  end

  @impl true
  def on_update(update) do

    # consume the update
    :ok
  end
end

Note: webhook_url must be a full URL, such as https://your.domain.com/updates_hook, where updates_hook path is fixed.

Summary

Callbacks

@callback on_boot() :: Telegex.Hook.Config.t()
@callback on_failure(
  update :: Telegex.Type.Update.t(),
  {e :: any(), stacktrace :: any()}
) :: no_return()
@callback on_update(Telegex.Type.Update.t()) :: :ok | Telegex.Chain.result()