~fnux/telegram-client-elixir-demo

2bae9c5010b7ad3b29736b1f973196157efd9889 — Timothée Floure 5 years ago a37ccd7
Somewhat allows to fetch 'chats' (groups)
3 files changed, 37 insertions(+), 18 deletions(-)

M lib/telegramclient/cli.ex
M lib/telegramclient/listener.ex
M mix.lock
M lib/telegramclient/cli.ex => lib/telegramclient/cli.ex +10 -4
@@ 26,9 26,10 @@ defmodule TelegramClient.CLI do
      "signin" -> sign_in()
      "signup" -> sign_up()
      "send" -> send()
      "contacts" -> contacts()
      "contacts" -> get_contacts()
      "chats" -> get_chats()
      "debug" -> debug()
      "gen-authkey" -> gen_authkey()
      "akgen" -> gen_authkey()
      "export" -> export_session()
      "import" -> import_session()
      "help" -> print_help()


@@ 49,7 50,7 @@ defmodule TelegramClient.CLI do
    * send : send a message
    * contacts : print the contact list
    * chats : print the chats list
    * gen-authkey : generate the authorization key
    * akgen : generate the authorization key
    * export : export session parameters
    * import : import session parameters
    * exit : exit this application


@@ 125,11 126,16 @@ defmodule TelegramClient.CLI do
    MTProto.Session.send session_id, query
  end

  def contacts do
  def get_contacts do
    session_id = Registry.get().session_id
    MTProto.send session_id, MTProto.API.Contacts.get_contacts
  end

  def get_chats do
    session_id = Registry.get().session_id
    MTProto.send session_id, MTProto.API.Messages.get_dialogs(0,0,1000)
  end

  def send do
    session_id = Registry.get().session_id
    dst = IO.gets("Please enter the ID of the recipient : ") |> String.trim

M lib/telegramclient/listener.ex => lib/telegramclient/listener.ex +25 -12
@@ 41,7 41,13 @@ defmodule TelegramClient.Listener do
        IO.puts "New message on chat #{chat} from #{sender} : #{content}"
      "gzip_packed" ->
        data = Map.get(msg, :result) |> Map.get(:packed_data)
        process_gzip_packed(data)
        dispatch(data)
      "contacts.contacts" ->
        process_contacts(msg)
      "messages.dialogs" ->
        process_dialogs(msg)
      "messages.dialogsSlice" ->
        process_dialogs(msg)
      "new_session_created" ->
        IO.puts "New Telegram session created !"
      "msg_container" ->


@@ 55,22 61,29 @@ defmodule TelegramClient.Listener do
      "msgs_ack" -> :noop #ignore
      _ ->
        IO.puts "-- Unknown message ! --"
        IO.inspect msg
        IO.inspect Map.get(msg, :name)
        IO.inspect msg, limit: :infinity
        IO.puts "-----------------------"
    end
  end

  defp process_gzip_packed(data) do
    if :users in Map.keys(data) do
      IO.puts "*** Received contact list ***"
      users = Map.get(data, :users)
      for user <- users do
        id = Map.get(user, :id)
        name = Map.get(user, :first_name) <> Map.get(user, :last_name)
        username = Map.get(user, :username)
        phone = Map.get(user, :phone)
  defp process_contacts(data) do
    IO.puts "*** Received contact list ***"
    users = Map.get(data, :users)
    for user <- users do
      id = Map.get(user, :id)
      name = Map.get(user, :first_name) <> Map.get(user, :last_name)
      username = Map.get(user, :username)
      phone = Map.get(user, :phone)

        IO.puts "User ##{id} : #{name} - @#{username} - #{phone}"
      IO.puts "User ##{id} : #{name} - @#{username} - #{phone}"
    end
  end

  def process_dialogs(msg) do
    for chat <- msg.chats do
      if chat.name == "chat" do
        IO.puts "Chat id##{chat.id} : #{chat.title}"
      end
    end
  end

M mix.lock => mix.lock +2 -2
@@ 1,5 1,5 @@
%{"earmark": {:hex, :earmark, "1.2.0", "bf1ce17aea43ab62f6943b97bd6e3dc032ce45d4f787504e3adf738e54b42f3a", [:mix], []},
  "json": {:hex, :json, "1.0.0", "4824dadb01e14395f65e9d18da294d534326d7eed77996f3c22f74a9870179eb", [:mix], [{:earmark, ">= 0.0.0", [hex: :earmark, optional: false]}]},
  "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], []},
  "telegram_mt": {:git, "https://github.com/fnux/telegram-mt-elixir.git", "78e84bbcb032f7bcdbc0eb631a314ebce8d80b22", []},
  "telegram_tl": {:hex, :telegram_tl, "0.1.0-beta", "30be4fb1f0e5c35078d816d18b966f2ac26724bc6cb1e80334562310e36c82d8", [:mix], [{:poison, "~> 3.1", [hex: :poison, optional: false]}]}}
  "telegram_mt": {:git, "https://github.com/fnux/telegram-mt-elixir.git", "e74d73a81599539370cc6bcc7e7d262f17831d6c", []},
  "telegram_tl": {:git, "https://github.com/fnux/telegram-tl-elixir.git", "937b46f9924da1eced05c36afcb970790a7593ec", []}}