Telegram.Webhook (telegram v2.1.0)
View SourceTelegram Webhook supervisor.
This modules can the used to start a webserver exposing a webhoook endpoint
where the telegram server can push updates for your BOT.
On start the webhook address for the BOT is posted to the telegram server
via the setWebHook
method.
Usage
WebServer adapter
Two Plug
compatible webserver are supported:
Telegram.WebServer.Bandit
(default): useBandit
Telegram.WebServer.Cowboy
: usePlug.Cowboy
You should configure the desired webserver adapter in you app configuration:
config :telegram,
webserver: Telegram.WebServer.Bandit
# OR
config :telegram,
webserver: Telegram.WebServer.Cowboy
and include in you dependencies one of:
{:plug_cowboy, "~> 2.5"}
# OR
{:bandit, "~> 1.0"}
Supervision tree
In you app supervision tree:
webhook_config = [
host: "myapp.public-domain.com",
port: 443,
local_port: 4_000
]
bot_config = [
token: Application.fetch_env!(:my_app, :token_counter_bot),
allowed_updates: [] # optional (refer to Telegram.Types.bot_opts())
]
children = [
{Telegram.Webhook, config: webhook_config, bots: [{MyApp.Bot, bot_config}]}
...
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
Ref
Direct Phoenix
/ Plug
integration
To integrate the webhook in a Phoenix
/ Plug
based application facing internet,
configure a nil
, telegram webserver
config :telegram,
webserver: nil
In you app supervision tree:
webhook_config = [
host: "myapp.public-domain.com",
port: 443
]
bot_config = [
token: Application.fetch_env!(:my_app, :token_counter_bot),
allowed_updates: [] # optional (refer to Telegram.Types.bot_opts())
]
children = [
{Telegram.Webhook, config: webhook_config, bots: [{MyApp.Bot, bot_config}]}
...
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
In your app Phoenix
/ Plug
router:
defmodule App.Router do
use Plug.Router
# ... my app routes ...
post "/__telegram_webhook__/:token" do
Telegram.Webhook.post_webhook(conn, token)
end
end
Summary
Types
@type config() :: [ host: String.t(), scheme: String.t(), port: :inet.port_number(), local_port: :inet.port_number(), max_connections: 1..100, set_webhook: boolean() ]
Webhook configuration.
scheme
: webhook server connection type - "http" or "https" (optional, default: https)host
: hostname of the webhook url (required)port
: port of the webhook url (optional, default: 443)local_port
: (backend) port of the application HTTP web server. Used only iftelegram.webhook
is configured (optional, default: 4000)max_connections
: maximum allowed number of simultaneous connections to the webhook for update delivery (optional, defaults 40)
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
@spec post_webhook(Plug.Conn.t(), Telegram.Types.token()) :: Plug.Conn.t()
This function can be used to process an incoming webhook request
if you opted to serve the webhook in your app via a Plug
router.
defmodule App.Router do
use Plug.Router
# ... my app routes ...
post "/__telegram_webhook__/:token" do
Telegram.Webhook.post_webhook(conn, token)
end
end
@spec start_link(config: config(), bots: [Telegram.Types.bot_spec()]) :: Supervisor.on_start()