summaryrefslogtreecommitdiff
path: root/lib/telegram.ex
blob: 289b913b485707d931809590c1e54baec1816784 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
defmodule Nola.Telegram do
  require Logger
  @behaviour Telegram.ChatBot

  def my_path() do
    "https://t.me/beauttebot"
  end

  def send_message(id, text, md2 \\ false) do
    md = if md2, do: "MarkdownV2", else: "Markdown"
    token = Keyword.get(Application.get_env(:nola, :telegram, []), :key)
    Telegram.Bot.ChatBot.Chat.Session.Supervisor.start_child(Nola.Telegram, id)
    Telegram.Api.request(token, "sendMessage", chat_id: id, text: text, parse_mode: "Markdown")
  end

  @impl Telegram.ChatBot
  def init(chat_id) when chat_id < 0 do
    case Nola.TelegramRoom.init(chat_id) do
      {:ok, state} -> {:ok, %{room_state: state}}
      _ -> :ignore
    end
  end

  def init(chat_id) do
    Logger.info("Telegram session starting: #{chat_id}")
    account = Nola.Account.find_meta_account("telegram-id", chat_id)
    account_id = if account, do: account.id
    {:ok, %{account: account_id}}
  end

  @impl Telegram.ChatBot
  def handle_update(update, token, %{room_state: room_state}) do
    {:ok, room_state} = Nola.TelegramRoom.handle_update(update, token, room_state)
    {:ok, %{room_state: room_state}}
  end

  def handle_update(
        %{"message" => m = %{"chat" => %{"type" => "private"}, "text" => text = "/start" <> _}},
        _token,
        state
      ) do
    text =
      "*Welcome to beautte!*\n\nQuery the bot on IRC and say \"enable-telegram\" to continue."

    send_message(m["chat"]["id"], text)
    {:ok, %{account: nil}}
  end

  def handle_update(
        %{"message" => m = %{"chat" => %{"type" => "private"}, "text" => text = "/enable" <> _}},
        _token,
        state
      ) do
    key =
      case String.split(text, " ") do
        ["/enable", key | _] -> key
        _ -> "nil"
      end

    # Handled message "1247435154:AAGnSSCnySn0RuVxy_SUcDEoOX_rbF6vdq0" %{"message" =>
    #  %{"chat" => %{"first_name" => "J", "id" => 2075406, "type" => "private", "username" => "ahref"},
    #    "date" => 1591027272, "entities" =>
    #    [%{"length" => 7, "offset" => 0, "type" => "bot_command"}],
    #    "from" => %{"first_name" => "J", "id" => 2075406, "is_bot" => false, "language_code" => "en", "username" => "ahref"},
    #    "message_id" => 11, "text" => "/enable salope"}, "update_id" => 764148578}
    account = Nola.Account.find_meta_account("telegram-validation-code", String.downcase(key))

    text =
      if account do
        net = Nola.Account.get_meta(account, "telegram-validation-target")
        Nola.Account.put_meta(account, "telegram-id", m["chat"]["id"])
        Nola.Account.put_meta(account, "telegram-username", m["chat"]["username"])
        Nola.Account.put_meta(account, "telegram-username", m["chat"]["username"])
        Nola.Account.delete_meta(account, "telegram-validation-code")
        Nola.Account.delete_meta(account, "telegram-validation-target")

        Nola.Irc.Connection.broadcast_message(
          net,
          account,
          "Telegram #{m["chat"]["username"]} account added!"
        )

        "Yay! Linked to account **#{account.name}**."
      else
        "Token invalid"
      end

    send_message(m["chat"]["id"], text)
    {:ok, %{account: account.id}}
  end

  # [debug] Unhandled update: %{"message" =>
  #  %{"chat" => %{"first_name" => "J", "id" => 2075406, "type" => "private", "username" => "ahref"},
  #    "date" => 1591096015,
  #    "from" => %{"first_name" => "J", "id" => 2075406, "is_bot" => false, "language_code" => "en", "username" => "ahref"},
  #    "message_id" => 29,
  #    "photo" => [
  #      %{"file_id" => "AgACAgQAAxkBAAMdXtYyz4RQqLcpOlR6xKK3w3NayHAAAnCzMRuL4bFSgl_cTXMl4m5G_T0kXQADAQADAgADbQADZVMBAAEaBA",
  #        "file_size" => 9544, "file_unique_id" => "AQADRv09JF0AA2VTAQAB", "height" => 95, "width" => 320},
  #      %{"file_id" => "AgACAgQAAxkBAAMdXtYyz4RQqLcpOlR6xKK3w3NayHAAAnCzMRuL4bFSgl_cTXMl4m5G_T0kXQADAQADAgADeAADZFMBAAEaBA",
  #        "file_size" => 21420, "file_unique_id" => "AQADRv09JF0AA2RTAQAB", "height" => 148, "width" => 501}]},
  #  "update_id" => 218161546}

  for type <- ~w(photo voice video document animation) do
    def handle_update(data = %{"message" => %{unquote(type) => _}}, token, state) do
      start_upload(unquote(type), data, token, state)
    end
  end

  # [debug] Unhandled update: %{"callback_query" =>
  #  %{
  #    "chat_instance" => "-7948978714441865930", "data" => "evolu.net/#dmz",
  #      "from" => %{"first_name" => "J", "id" => 2075406, "is_bot" => false, "language_code" => "en", "username" => "ahref"},
  #      "id" => "8913804780149600",
  #       "message" => %{"chat" => %{"first_name" => "J", "id" => 2075406, "type" => "private", "username" => "ahref"},
  #                    "date" => 1591098553, "from" => %{"first_name" => "devbeautte", "id" => 1293058838, "is_bot" => true, "username" => "devbeauttebot"},
  #                    "message_id" => 62,
  #                    "reply_markup" => %{"inline_keyboard" => [[%{"callback_data" => "random/#", "text" => "random/#"},
  #                          %{"callback_data" => "evolu.net/#dmz", "text" => "evolu.net/#dmz"}]]},
  #                    "text" => "Where should I send the file?"}
  #   }
  #  , "update_id" => 218161568}

  # def handle_update(t, %{"callback_query" => cb = %{"data" => "resend", "id" => id, "message" => m = %{"message_id" => m_id, "chat" => %{"id" => chat_id}, "reply_to_message" => op}}}) do
  # end

  def handle_update(
        %{
          "callback_query" =>
            cb = %{
              "data" => "start-upload:" <> target,
              "id" => id,
              "message" =>
                m = %{
                  "message_id" => m_id,
                  "chat" => %{"id" => chat_id},
                  "reply_to_message" => op
                }
            }
        },
        t,
        state
      ) do
    account = Nola.Account.find_meta_account("telegram-id", chat_id)

    if account do
      target =
        case String.split(target, "/") do
          ["everywhere"] -> Nola.Membership.of_account(account)
          [net, chan] -> [{net, chan}]
        end

      Telegram.Api.request(t, "editMessageText",
        chat_id: chat_id,
        message_id: m_id,
        text: "Processing...",
        reply_markup: %{}
      )

      {content, type} =
        cond do
          op["photo"] -> {op["photo"], ""}
          op["voice"] -> {op["voice"], " a voice message"}
          op["video"] -> {op["video"], ""}
          op["document"] -> {op["document"], ""}
          op["animation"] -> {op["animation"], ""}
        end

      file =
        if is_list(content) && Enum.count(content) > 1 do
          Enum.sort_by(content, fn p -> p["file_size"] end, &>=/2)
          |> List.first()
        else
          content
        end

      file_id = file["file_id"]
      file_unique_id = file["file_unique_id"]
      text = if(op["caption"], do: ": " <> op["caption"] <> "", else: "")
      resend = %{"inline_keyboard" => [[%{"text" => "re-share", "callback_data" => "resend"}]]}

      spawn(fn ->
        with {:ok, file} <- Telegram.Api.request(t, "getFile", file_id: file_id),
             path = "https://api.telegram.org/file/bot#{t}/#{file["file_path"]}",
             {:ok, %HTTPoison.Response{status_code: 200, body: body}} <- HTTPoison.get(path),
             <<smol_body::binary-size(20), _::binary>> = body,
             {:ok, magic} <- GenMagic.Pool.perform(Nola.GenMagic, {:bytes, smol_body}),
             bucket = Application.get_env(:nola, :s3, []) |> Keyword.get(:bucket),
             ext = Path.extname(file["file_path"]),
             s3path = "#{account.id}/#{file_unique_id}#{ext}",
             Telegram.Api.request(t, "editMessageText",
               chat_id: chat_id,
               message_id: m_id,
               text: "*Uploading...*",
               reply_markup: %{},
               parse_mode: "MarkdownV2"
             ),
             s3req =
               ExAws.S3.put_object(bucket, s3path, body,
                 acl: :public_read,
                 content_type: magic.mime_type
               ),
             {:ok, _} <- ExAws.request(s3req) do
          path = NolaWeb.Router.Helpers.url(NolaWeb.Endpoint) <> "/files/#{s3path}"

          sent =
            for {net, chan} <- target do
              txt = "sent#{type}#{text} #{path}"
              Nola.Irc.send_message_as(account, net, chan, txt)
              "#{net}/#{chan}"
            end

          if caption = op["caption"], do: as_irc_message(chat_id, caption, account)
          text = "Sent on " <> Enum.join(sent, ", ") <> " !"

          Telegram.Api.request(t, "editMessageText",
            chat_id: chat_id,
            message_id: m_id,
            text: "_Sent!_",
            reply_markup: %{},
            parse_mode: "MarkdownV2"
          )
        else
          error ->
            Telegram.Api.request(t, "editMessageText",
              chat_id: chat_id,
              message_id: m_id,
              text: "Something failed.",
              reply_markup: %{},
              parse_mode: "MarkdownV2"
            )

            Logger.error("Failed upload from Telegram: #{inspect(error)}")
        end
      end)
    end

    {:ok, state}
  end

  def handle_update(
        %{"message" => m = %{"chat" => %{"id" => id, "type" => "private"}, "text" => text}},
        _,
        state
      ) do
    account = Nola.Account.find_meta_account("telegram-id", id)

    if account do
      as_irc_message(id, text, account)
    end

    {:ok, state}
  end

  def handle_update(m, _, state) do
    Logger.debug("Unhandled update: #{inspect(m)}")
    {:ok, state}
  end

  @impl Telegram.ChatBot
  def handle_info(info, %{room_state: room_state}) do
    {:ok, room_state} = Nola.TelegramRoom.handle_info(info, room_state)
    {:ok, %{room_state: room_state}}
  end

  def handle_info(_info, state) do
    {:ok, state}
  end

  defp as_irc_message(id, text, account) do
    reply_fun = fn text -> send_message(id, text) end

    trigger_text =
      cond do
        String.starts_with?(text, "/") ->
          "/" <> text = text
          "!" <> text

        Enum.any?(Nola.Irc.Connection.triggers(), fn {trigger, _} ->
          String.starts_with?(text, trigger)
        end) ->
          text

        true ->
          "!" <> text
      end

    message = %Nola.Message{
      id: FlakeId.get(),
      transport: :telegram,
      network: "telegram",
      channel: nil,
      text: text,
      account: account,
      sender: %ExIRC.SenderInfo{nick: account.name},
      replyfun: reply_fun,
      trigger: Nola.Irc.Connection.extract_trigger(trigger_text),
      at: nil
    }

    Nola.Irc.Connection.publish(message, [
      "messages:private",
      "messages:telegram",
      "telegram/#{account.id}:messages"
    ])

    message
  end

  defp start_upload(
         _type,
         %{"message" => m = %{"chat" => %{"id" => id, "type" => "private"}}},
         token,
         state
       ) do
    account = Nola.Account.find_meta_account("telegram-id", id)

    if account do
      text = if(m["text"], do: m["text"], else: nil)

      targets =
        Nola.Membership.of_account(account)
        |> Enum.map(fn {net, chan} -> "#{net}/#{chan}" end)
        |> Enum.map(fn i -> %{"text" => i, "callback_data" => "start-upload:#{i}"} end)

      kb =
        if Enum.count(targets) > 1 do
          [%{"text" => "everywhere", "callback_data" => "start-upload:everywhere"}] ++ targets
        else
          targets
        end
        |> Enum.chunk_every(2)

      keyboard = %{"inline_keyboard" => kb}

      Telegram.Api.request(token, "sendMessage",
        chat_id: id,
        text: "Where should I send this file?",
        reply_markup: keyboard,
        reply_to_message_id: m["message_id"],
        parse_mode: "MarkdownV2"
      )
    end

    {:ok, state}
  end
end