Current section
Files
Jump to
Current section
Files
lib/test.ex
defmodule EchoBot do
use TelegramEx, name: :echo_bot
def handle_message(%{text: "/start", chat: chat}) do
Message.text("Здравствуйте. Введите свой E-mail")
|> Message.reply_keyboard([[%{text: "Отмена"}]], resize_keyboard: true)
|> Message.send(chat["id"])
{:transition, :waiting_email, %{}}
end
defstate :waiting_email do
def handle_message(%{text: "Отмена", chat: chat}) do
cancel(chat["id"])
end
def handle_message(%{text: email, chat: chat}, data) do
if String.match?(email, ~r/^[^\s@]+@[^\s@]+\.[^\s@]+$/) do
Message.text("*#{email}*\n\nВсё верно?", "Markdown")
|> Message.remove_keyboard()
|> Message.inline_keyboard([
[
%{text: "Да", callback_data: "confirm"},
%{text: "Заново", callback_data: "restart"}
]
])
|> Message.send(chat["id"])
{:transition, :confirming, Map.put(data, :email, email)}
else
Message.text("Некорректный email, попробуй ещё раз.")
|> Message.send(chat["id"])
end
end
end
def handle_callback(%{id: id, data: "confirm", message: %{chat: chat}}) do
Message.text("Готово, регистрация завершена!")
|> Message.answer_callback_query(id)
|> Message.send(chat["id"])
FSM.reset_state(:echo_bot, chat["id"])
:ok
end
def handle_callback(%{id: id, data: "restart", message: %{chat: chat}}) do
Message.text("Введите ваш E-mail")
|> Message.answer_callback_query(id)
|> Message.reply_keyboard([[%{text: "Отмена"}]], resize_keyboard: true)
|> Message.send(chat["id"])
{:transition, :waiting_email, %{}}
end
defp cancel(chat_id) do
Message.text("Регистрация отменена.")
|> Message.remove_keyboard()
|> Message.send(chat_id)
FSM.reset_state(:echo_bot, chat_id)
:ok
end
end