Magik.TelegramNoti (Magik v0.10.0) View Source

This module provide some helper functions that help to send message to a telegram conversation

Config

config :magik, :telegram_noti,
    bot_token: "your bot token".
    conversations: [
          default: "default_chat_id",
          admin: "other chat id"
      ],
    mode: :prod # or :dev
  • bot_token: your Telegram bot tokent
  • conversations: keyword list of name and chat_id. There must be at least 1 conversation which is :default. default is used if you don't specify conversation name in the function call.

Then you are ready to send message to your Telegram conversation

Link to this section Summary

Functions

Format and send error message to a Telegam conversation with data from a connection. This helper is used to send error from your phoenix router or controller.

Format error and send to Telegram conversation.

Send a message to conversation

This macro help you to catch exceptions and then send to your Telegram conversation using send_error/4

Link to this section Functions

Link to this function

send_conn_error(conversation \\ :default, conn, map)

View Source

Specs

send_conn_error(atom(), Plug.Conn.t(), map()) :: {:ok, map()} | {:error, map()}

Format and send error message to a Telegam conversation with data from a connection. This helper is used to send error from your phoenix router or controller.

From router

defmodule MyApp.Router do
    use MyAppWeb, :router
    use Plug.ErrorHandler
    ...

    def handle_errors(conn, error) do
        if conn.status >= 500 do
             Magik.TelegramNoti.send_conn_error(:api, conn, error)
        end
        ....
   end
end

from controller

defmodule MyAppWeb.PageController do
    ...
    def index(conn, params)do
      try do
          ...
      catch
        error ->
           Magik.TelegramNoti.send_conn_error(:api, conn, %{kind: :error, reason: error, stack: __STACKTRACE__})
           # return your error
      end
    end
end
Link to this function

send_error(conversation \\ :default, title, args \\ nil, map)

View Source

Specs

send_error(atom(), String.t(), any(), map()) :: {:ok, map()} | {:error, map()}

Format error and send to Telegram conversation.

defmodule MyApp.Calculator do
    ...
    def divide(a, b)do
        try do
            ...
        catch
            error ->
                Magik.TelegramNoti.send_error(:api, "MyApp.Calculator error", [a,b], %{kind: :error, reason: error, stack: __STACKTRACE__})
                # return your error
        end
    end
end
Link to this function

send_message(conversation \\ :default, message)

View Source

Specs

send_message(atom(), String.t()) :: {:ok, map()} | {:error, map()}

Send a message to conversation

send_message(:api, "this is a sample message")
Link to this macro

watch(opts \\ [], list)

View Source (macro)

This macro help you to catch exceptions and then send to your Telegram conversation using send_error/4

Options

  • to: conversation name from your config. Default is :default
  • args: argument list that passed to function, this is sent to telegram chat for dev to debug easier. If not speficied, arguments for current function call are used
  • label: label for this error/function. If not specified, current function name is used.

Example

...
require Magik.TelegramNoti

def do_something(args) do
  Magik.TelegramNoti.watch [to: :admin] do
      # your logic code here
  end
end
...