From 23a35b99c5768528d89037bbf372723affcbf7b1 Mon Sep 17 00:00:00 2001
From: visciang Download a file. Reference: BOT Api Example: In you app supervisor tree: In you app supervisor tree: and include in you dependencies one of: and include in you dependencies one of: In you app supervision tree: In you app supervision tree: The package can be installed by adding This module expose a light layer over the Telegram Bot API HTTP-based interface,
+ The package can be installed by adding This module expose a light layer over the Telegram Bot API HTTP-based interface,
it does not expose any "(data)binding" over the HTTP interface and tries to abstract
away only the boilerplate for building / sending / serializing the API requests. Compared to a full data-binded interface it could result less "typed frendly" but it will
work with any version of the Bot API, hopefully without updates or incompatibily
@@ -147,52 +147,52 @@ To download a file from the telegram server you need a
Telegram.Api
- (telegram v1.2.2)
+ (telegram v1.2.3)
@@ -369,15 +369,15 @@ file(token, file_path)
+{:ok, %{"file_path" => file_path}} = Telegram.Api.request(token, "getFile", file_id: file_id)
+{:ok, file} = Telegram.Api.file(token, file_path)
# send a photo
-{:ok, res} = Telegram.Api.request(token, "sendPhoto", chat_id: 12345, photo: {:file, "example/photo.jpg"})
+{:ok, res} = Telegram.Api.request(token, "sendPhoto", chat_id: 12345, photo: {:file, "example/photo.jpg"})
# pick the 'file_obj' with the desired resolution
-[file_obj | _] = res["photo"]
+[file_obj | _] = res["photo"]
# get the 'file_id'
-file_id = file_obj["file_id"]
+file_id = file_obj["file_id"]
# obtain the 'file_path' to download the file identified by 'file_id'
-{:ok, %{"file_path" => file_path}} = Telegram.Api.request(token, "getFile", file_id: file_id)
-{:ok, file} = Telegram.Api.file(token, file_path)
Telegram.Bot.Dispatch behaviour
- (telegram v1.2.2)
+ (telegram v1.2.3)
diff --git a/Telegram.Bot.Utils.html b/Telegram.Bot.Utils.html
index 77756e1..43a9ce9 100644
--- a/Telegram.Bot.Utils.html
+++ b/Telegram.Bot.Utils.html
@@ -5,10 +5,10 @@
-
+
-
Telegram.Bot.Utils
- (telegram v1.2.2)
+ (telegram v1.2.3)
diff --git a/Telegram.Bot.html b/Telegram.Bot.html
index 71e2404..bd6d566 100644
--- a/Telegram.Bot.html
+++ b/Telegram.Bot.html
@@ -5,10 +5,10 @@
-
+
-
Telegram.Bot behaviour
- (telegram v1.2.2)
+ (telegram v1.2.3)
@@ -130,27 +130,27 @@
Example
-
defmodule HelloBot do
+
+ end
+enddefmodule HelloBot do
use Telegram.Bot
@impl Telegram.Bot
- def handle_update(
- %{"message" => %{"text" => "/hello", "chat" => %{"id" => chat_id, "username" => username}, "message_id" => message_id}},
+ def handle_update(
+ %{"message" => %{"text" => "/hello", "chat" => %{"id" => chat_id, "username" => username}, "message_id" => message_id}},
token
- ) do
- Telegram.Api.request(token, "sendMessage",
+ ) do
+ Telegram.Api.request(token, "sendMessage",
chat_id: chat_id,
reply_to_message_id: message_id,
- text: "Hello #{username}!"
- )
- end
+ text: "Hello #{username}!"
+ )
+ end
- def handle_update(_update, _token) do
+ def handle_update(_update, _token) do
# ignore unknown updates
:ok
- end
-end
Telegram.ChatBot behaviour
- (telegram v1.2.2)
+ (telegram v1.2.3)
@@ -136,52 +136,52 @@
Example
-
diff --git a/Telegram.Poller.html b/Telegram.Poller.html
index ebb6c2b..1af6b7d 100644
--- a/Telegram.Poller.html
+++ b/Telegram.Poller.html
@@ -5,10 +5,10 @@
-
+
- defmodule HelloBot do
+
+ {:stop, count_state}
+ end
+enddefmodule HelloBot do
use Telegram.ChatBot
@session_ttl 60 * 1_000
@impl Telegram.ChatBot
- def init(_chat) do
+ def init(_chat) do
count_state = 0
- {:ok, count_state, @session_ttl}
- end
+ {:ok, count_state, @session_ttl}
+ end
@impl Telegram.ChatBot
- def handle_update(%{"message" => %{"chat" => %{"id" => chat_id}}}, token, count_state) do
+ def handle_update(%{"message" => %{"chat" => %{"id" => chat_id}}}, token, count_state) do
count_state = count_state + 1
- Telegram.Api.request(token, "sendMessage",
+ Telegram.Api.request(token, "sendMessage",
chat_id: chat_id,
- text: "Hey! You sent me #{count_state} messages"
- )
+ text: "Hey! You sent me #{count_state} messages"
+ )
- {:ok, count_state, @session_ttl}
- end
+ {:ok, count_state, @session_ttl}
+ end
- def handle_update(update, _token, count_state) do
+ def handle_update(update, _token, count_state) do
# ignore unknown updates
- {:ok, count_state, @session_ttl}
- end
+ {:ok, count_state, @session_ttl}
+ end
@impl Telegram.ChatBot
- def handle_info(msg, _token, _chat_id, count_state) do
+ def handle_info(msg, _token, _chat_id, count_state) do
# direct message processing
- {:ok, count_state}
- end
+ {:ok, count_state}
+ end
@impl Telegram.ChatBot
- def handle_timeout(token, chat_id, count_state) do
- Telegram.Api.request(token, "sendMessage",
+ def handle_timeout(token, chat_id, count_state) do
+ Telegram.Api.request(token, "sendMessage",
chat_id: chat_id,
text: "See you!"
- )
+ )
- {:stop, count_state}
- end
-end
Telegram.Poller
- (telegram v1.2.2)
+ (telegram v1.2.3)
@@ -130,18 +130,18 @@
Usage
-
diff --git a/Telegram.Types.html b/Telegram.Types.html
index 9750476..c221513 100644
--- a/Telegram.Types.html
+++ b/Telegram.Types.html
@@ -5,10 +5,10 @@
-
+
- bot_config = [
- token: Application.fetch_env!(:my_app, :token_counter_bot),
- max_bot_concurrency: Application.fetch_env!(:my_app, :max_bot_concurrency)
-]
+
+opts = [strategy: :one_for_one, name: MyApp.Supervisor]
+Supervisor.start_link(children, opts)bot_config = [
+ token: Application.fetch_env!(:my_app, :token_counter_bot),
+ max_bot_concurrency: Application.fetch_env!(:my_app, :max_bot_concurrency)
+]
-children = [
- {Telegram.Poller, bots: [{MyApp.Bot, bot_config}]}
+children = [
+ {Telegram.Poller, bots: [{MyApp.Bot, bot_config}]}
...
-]
+]
-opts = [strategy: :one_for_one, name: MyApp.Supervisor]
-Supervisor.start_link(children, opts)
Telegram.Types
- (telegram v1.2.2)
+ (telegram v1.2.3)
diff --git a/Telegram.WebServer.Bandit.html b/Telegram.WebServer.Bandit.html
index f68788c..b07bcf7 100644
--- a/Telegram.WebServer.Bandit.html
+++ b/Telegram.WebServer.Bandit.html
@@ -5,10 +5,10 @@
-
+
-
Telegram.WebServer.Bandit
- (telegram v1.2.2)
+ (telegram v1.2.3)
diff --git a/Telegram.WebServer.Cowboy.html b/Telegram.WebServer.Cowboy.html
index 1292ee0..8a5f271 100644
--- a/Telegram.WebServer.Cowboy.html
+++ b/Telegram.WebServer.Cowboy.html
@@ -5,10 +5,10 @@
-
+
-
Telegram.WebServer.Cowboy
- (telegram v1.2.2)
+ (telegram v1.2.3)
diff --git a/Telegram.Webhook.html b/Telegram.Webhook.html
index ef948f3..b6a1d7d 100644
--- a/Telegram.Webhook.html
+++ b/Telegram.Webhook.html
@@ -5,10 +5,10 @@
-
+
-
Telegram.Webhook
- (telegram v1.2.2)
+ (telegram v1.2.3)
@@ -144,34 +144,34 @@
# OR
config :telegram,
- webserver: Telegram.WebServer.Cowboy
{:plug_cowboy, "~> 2.5"}
+ webserver: Telegram.WebServer.Cowboy
{:plug_cowboy, "~> 2.5"}
# OR
-{:bandit, "~> 1.0"}
+{:bandit, "~> 1.0"}
Supervision tree
-webhook_config = [
+
webhook_config = [
host: "myapp.public-domain.com",
port: 443,
local_port: 4_000
-]
+]
-bot_config = [
- token: Application.fetch_env!(:my_app, :token_counter_bot),
- max_bot_concurrency: Application.fetch_env!(:my_app, :max_bot_concurrency)
-]
+bot_config = [
+ token: Application.fetch_env!(:my_app, :token_counter_bot),
+ max_bot_concurrency: Application.fetch_env!(:my_app, :max_bot_concurrency)
+]
-children = [
- {Telegram.Webhook, config: webhook_config, bots: [{MyApp.Bot, bot_config}]}
+children = [
+ {Telegram.Webhook, config: webhook_config, bots: [{MyApp.Bot, bot_config}]}
...
-]
+]
-opts = [strategy: :one_for_one, name: MyApp.Supervisor]
-Supervisor.start_link(children, opts)
+
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
+Supervisor.start_link(children, opts)
diff --git a/api-reference.html b/api-reference.html
index c70104e..d955f97 100644
--- a/api-reference.html
+++ b/api-reference.html
@@ -5,10 +5,10 @@
-
+
-
-
- API Reference telegram v1.2.2
+ API Reference telegram v1.2.3
diff --git a/index.html b/index.html
index 0f250b1..e789ce8 100644
--- a/index.html
+++ b/index.html
@@ -2,7 +2,7 @@
-
Installation
telegram
to your list of dependencies in mix.exs
:def deps do
- [
- {:telegram, github: "visciang/telegram", tag: "xxx"}
- ]
-end
Telegram Bot API
telegram
to your list of dependencies in mix.exs
:def deps do
+ [
+ {:telegram, github: "visciang/telegram", tag: "xxx"}
+ ]
+end
Telegram Bot API
getMe
-
Telegram.Api.request(token, "getMe")
+
Telegram.Api.request(token, "getMe")
-{:ok, %{"first_name" => "Abc", "id" => 1234567, "is_bot" => true, "username" => "ABC"}}
+
{:ok, %{"first_name" => "Abc", "id" => 1234567, "is_bot" => true, "username" => "ABC"}}
sendMessage
-Telegram.Api.request(token, "sendMessage", chat_id: 876532, text: "Hello! .. silently", disable_notification: true)
+
Telegram.Api.request(token, "sendMessage", chat_id: 876532, text: "Hello! .. silently", disable_notification: true)
-{:ok,
- %{"chat" => %{"first_name" => "Firstname",
+{:ok,
+ %{"chat" => %{"first_name" => "Firstname",
"id" => 208255328,
"last_name" => "Lastname",
"type" => "private",
- "username" => "xxxx"},
+ "username" => "xxxx"},
"date" => 1505118722,
- "from" => %{"first_name" => "Yyy",
+ "from" => %{"first_name" => "Yyy",
"id" => 234027650,
"is_bot" => true,
- "username" => "yyy"},
+ "username" => "yyy"},
"message_id" => 1402,
- "text" => "Hello! .. silently"}}
+
"text" => "Hello! .. silently"}}
getUpdates
-Telegram.Api.request(token, "getUpdates", offset: -1, timeout: 30)
+
Telegram.Api.request(token, "getUpdates", offset: -1, timeout: 30)
-{:ok,
- [%{"message" => %{"chat" => %{"first_name" => "Firstname",
+{:ok,
+ [%{"message" => %{"chat" => %{"first_name" => "Firstname",
"id" => 208255328,
"last_name" => "Lastname",
"type" => "private",
- "username" => "xxxx"},
+ "username" => "xxxx"},
"date" => 1505118098,
- "from" => %{"first_name" => "Firstname",
+ "from" => %{"first_name" => "Firstname",
"id" => 208255328,
"is_bot" => false,
"language_code" => "en-IT",
"last_name" => "Lastname",
- "username" => "xxxx"},
+ "username" => "xxxx"},
"message_id" => 1401,
- "text" => "Hello!"},
- "update_id" => 129745295}]}
+
"text" => "Hello!"},
+ "update_id" => 129745295}]}
@@ -207,26 +207,26 @@
sendPhoto
-
Telegram.Api.request(token, "sendPhoto", chat_id: 876532, photo: {:file, "/tmp/photo.jpg"})
-Telegram.Api.request(token, "sendPhoto", chat_id: 876532, photo: {:file_content, photo, "photo.jpg"})
+
Telegram.Api.request(token, "sendPhoto", chat_id: 876532, photo: {:file, "/tmp/photo.jpg"})
+Telegram.Api.request(token, "sendPhoto", chat_id: 876532, photo: {:file_content, photo, "photo.jpg"})
Downloading files
file_path
pointer to the file.
-With that you can download the file via Telegram.Api.file
.{:ok, res} = Telegram.Api.request(token, "sendPhoto", chat_id: 12345, photo: {:file, "example/photo.jpg"})
+With that you can download the file via
Telegram.Api.file
.
{:ok, res} = Telegram.Api.request(token, "sendPhoto", chat_id: 12345, photo: {:file, "example/photo.jpg"})
# pick the 'file_obj' with the desired resolution
-[file_obj | _] = res["photo"]
+[file_obj | _] = res["photo"]
# get the 'file_id'
-file_id = file_obj["file_id"]
{:ok, %{"file_path" => file_path}} = Telegram.Api.request(token, "getFile", file_id: file_id)
-{:ok, file} = Telegram.Api.file(token, file_path)
{:ok, %{"file_path" => file_path}} = Telegram.Api.request(token, "getFile", file_id: file_id)
+{:ok, file} = Telegram.Api.file(token, file_path)
keyboard = [
- ["A0", "A1"],
- ["B0", "B1", "B2"]
-]
-keyboard_markup = %{one_time_keyboard: true, keyboard: keyboard}
-Telegram.Api.request(token, "sendMessage", chat_id: 876532, text: "Here a keyboard!", reply_markup: {:json, keyboard_markup})
keyboard = [
+ ["A0", "A1"],
+ ["B0", "B1", "B2"]
+]
+keyboard_markup = %{one_time_keyboard: true, keyboard: keyboard}
+Telegram.Api.request(token, "sendMessage", chat_id: 876532, text: "Here a keyboard!", reply_markup: {:json, keyboard_markup})
This mode can be used in a dev environment or if your bot doesn't need to "scale". Being in pull it works well behind a firewall (or behind a home internet router).
Refer to the Telegram.Poller
module docs for more info.
The Telegram HTTP Client is based on Tesla
.
The Tesla.Adapter
and options should be configured via the [:tesla, :adapter]
application environment key.
-(ref. https://hexdocs.pm/tesla/readme.html#adapters)
For example, a good default could be:
config :tesla, adapter: {Tesla.Adapter.Hackney, [recv_timeout: 40_000]}
a dependency should be added accordingly in your mix.exs
:
defp deps do
- [
- {:telegram, github: "visciang/telegram", tag: "xxx"},
- {:hackney, "~> 1.18"},
+(ref. https://hexdocs.pm/tesla/readme.html#adapters)For example, a good default could be:
config :tesla, adapter: {Tesla.Adapter.Hackney, [recv_timeout: 40_000]}
a dependency should be added accordingly in your mix.exs
:
defp deps do
+ [
+ {:telegram, github: "visciang/telegram", tag: "xxx"},
+ {:hackney, "~> 1.18"},
# ...
- ]
- end
+
]
+ end