Giter Site home page Giter Site logo

ex-telegram-client's Introduction

Telegram Client

A Elixir wrapper that communicates with the Telegram-CLI.

Installation

If available in Hex, the package can be installed as:

  • Add tg_client to your list of dependencies in mix.exs:
def deps do
  [{:tg_client, "~> 0.1.0"]
end
  • Ensure tg_client is started before your application:
def application do
  [applications: [:tg_client]]
end

Usage

  • Write your own EventHandler module.
defmodule EventHandler do
  use GenEvent
  require Logger

  def handle_event(event, state) do
    Logger.debug "Unknown Event: " <> inspect(event)
    {:ok, state}
  end
end
  • Set config.
config :tg_client,
  daemon: "/usr/local/telegram-cli",
  key: "/usr/local/share/telegram-cli/tg-server.pub",
  session_env_path: "/tmp/telegram-cli/sessions",
  port_range: 2000..4000,
  default_pool_size: 5,
  default_pool_max_overflow: 10,
  pool_name: :event_handler,
  event_handler: {TgClient.Event.Handler, size: 10, max_overflow: 10}

Authorization

  • Start session
{:ok, _pid} = TgClient.Session.start_link(79251008050)
  • Check session status
{:ok, status} = TgClient.Session.current_status(79251008050)

when status in [:init, :waiting_for_confirmation, :waiting_for_password, :connected]

  • Confirm
:ok = TgClient.Session.confirm(79251008050, 22284)

has effect only if status is :waiting_for_confirmation

  • Put password
:ok = TgClient.Session.put_password(79251008050, "secret_password")

has effect only if status is :waiting_for_password

Commands

all commands has effect only if status is :connected

  • Dialog list
{:ok, response} = TgClient.Session.send_command(79251008050, "dialog_list", [])

when response is:

[
   {
      \"admin\":{
         \"id\":\"$01000000000000000000000000000000\",
         \"peer_type\":\"user\",
         \"peer_id\":0,
         \"print_name\":\"user#0\"
      },
      \"id\":\"$02000000e47cd0010000000000000000\",
      \"flags\":1,
      \"peer_type\":\"chat\",
      \"peer_id\":30440676,
      \"print_name\":\"йцу\",
      \"title\":\"йцу\",
      \"members_num\":3
   },
   {
      \"id\":\"$010000005d7c21050ddae73d36d42000\",
      \"peer_type\":\"user\",
      \"flags\":196609,
      \"peer_id\":86080605,
      \"first_name\":\"Собакин\",
      \"print_name\":\"Собакин_Кот\",
      \"when\": \"2016-08-31 22:45:43\",
      \"phone\":\"7800400300\",
      \"last_name\":\"Кот\"
   }
]
  • Contact list
{:ok, response} = TgClient.Session.send_command(79251008050, "contact_list", [])

when response is:

[
   {
      \"id\":\"$01000000946c090ddf6ba9457ce8c248\",
      \"peer_type\":\"user\",
      \"flags\":196609,
      \"peer_id\":218721428,
      \"first_name\":\"Дмитрий\",
      \"print_name\":\"Дмитрий_Негру\",
      \"when\": \"2016-05-20 18:12:40\",
      \"phone\":\"7800400300\",
      \"last_name\":\"Негру\"
   }
]
  • Send message [peer, text]
{:ok, response} = TgClient.Session.send_command(79251008050, "msg", ["$010000001az3av003d8059e845e429e1", "hello"])

when response is:

{  
   \"result\":\"SUCCESS\"
}
  • Message history [peer, limit, offset]
{:ok, response} = TgClient.Session.send_command(79251008050, "history", ["$010000001az3av003d8059e845e429e1", "5", "0"])

when response is:

[
   {
      \"text\":\"hello\",
      \"unread\":true,
      \"event\":\"message\",
      \"id\":\"010000001ae3ab00496c0100000000003d8059e845e429e1\",
      \"from\":{
         \"username\":\"badrequest\",
         \"id\":\"$010000001ae3ab003d8059e845e429e1\",
         \"peer_type\":\"user\",
         \"flags\":524289,
         \"peer_id\":11264794,
         \"first_name\":\"Andrew\",
         \"print_name\":\"Andrew_Noskov\",
         \"when\":\"2016-09-01 14:06:33\",
         \"phone\":\"79251008050\",
         \"last_name\":\"Noskov\"
      },
      \"flags\":16643,
      \"to\":{
         \"username\":\"badrequest\",
         \"id\":\"$010000001ae3ab003d8059e845e429e1\",
         \"peer_type\":\"user\",
         \"flags\":524289,
         \"peer_id\":11264794,
         \"first_name\":\"Andrew\",
         \"print_name\":\"Andrew_Noskov\",
         \"when\":\"2016-09-01 14:06:33\",
         \"phone\":\"79251008050\",
         \"last_name\":\"Noskov\"
      },
      \"out\":true,
      \"service\":false,
      \"date\":1472727693
   }
]
  • Create secret chat [peer]
{:ok, response} = TgClient.Session.send_command(79251008050, "create_secret_chat", ["$010000001az3av003d8059e845e429e1"])

when response is:

{
   \"flags\":1,
   \"print_name\":\"!_Polya\",
   \"id\":\"$010000001az3av003d8059e845e429e1\",
   \"peer_type\":\"encr_chat\",
   \"peer_id\":1901915394,
   \"user\":{
      \"flags\":196609,
      \"id\":\"$010000001az3av003d8059e845e429e1\",
      \"print_name\":\"Polya\",
      \"peer_type\":\"user\",
      \"last_name\":\"\",
      \"peer_id\":65370635,
      \"first_name\":\"Polya\",
      \"when\":\"2016-09-01 13:35:34\",
      \"phone\":\"79251008050\"
   }
}

Configuration

  • Set config or pass default attributes
config :tg_client,
  daemon: "/usr/local/telegram-cli",
  key: "/usr/local/share/telegram-cli/tg-server.pub",
  session_env_path: "/tmp/telegram-cli/sessions",
  port_range: 2000..4000,
  default_pool_size: 5,
  default_pool_max_overflow: 10,
  pool_name: :event_handler,
  event_handler: {TgClient.Event.Handler, size: 10, max_overflow: 10}

ex-telegram-client's People

Contributors

anoskov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.