rockneurotiko/ex_gram

Catch errors

Closed this issue · 2 comments

Hi. How I can catch some errors if I send message via answer?
Imagine, I send document with wrong id:

context |> answer_document("SOME_WRONG_ID")

and document not sended. And I want to send document via file_path.

context |> answer_document({:file, "/some/path/to/file.pdf"})

How I can do it? ))

The answers that are done via the context are executed after the handle/2 method ends. And all the errors that happens when calling the API will be sent to the method handle_error/1 that you can override on your bot.

The thing is that I'm not sure if the error will be enough to know the chat to send the file, and you won't be able to use answer_document because in the handle_error/1 method there are no context, you will need to use ExGram.send_document

Instead of that you can use ExGram.send_document instead of answer_document, something like this:

defp send_document_with_fallback(ctx, document_id, document_path) do
  chat_id = ExGram.Dsl.extract_id(ctx.update)
  case ExGram.send_document(chat_id, document_id, bot: @bot_name) do
    {:error, _} -> ExGram.send_document(chat_id, {:file, document_path}, bot: @bot_name)
    _ -> :ok
  end

  ctx
end

Yeah. Thank you )