Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telegram bot UI (#3) #4

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fmt
  • Loading branch information
mmvpm committed Mar 17, 2024
commit a5c8cd86aa8cc9bbc5fd8803c4363f14d1b6e676
16 changes: 8 additions & 8 deletions bot/src/main/scala/com/github/mmvpm/bot/OfferServiceBot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ class OfferServiceBot[F[_]: Concurrent](
renderer: Renderer,
manager: StateManager[F],
stateStorage: Storage[State],
lastMessageStorage: Storage[Option[MessageID]])
extends TelegramBot[F](token, sttpBackend)
with Polling[F]
with Commands[F]
with Callbacks[F] {
lastMessageStorage: Storage[Option[MessageID]]
) extends TelegramBot[F](token, sttpBackend)
with Polling[F]
with Commands[F]
with Callbacks[F] {

// user sent a message (text, image, etc) to the chat
onMessage { implicit message =>
command(message) match {
case Some(Command("roll", _)) => roll
case Some(Command("roll", _)) => roll
case Some(Command("start", _)) => start
case None =>
getNextStateTag(stateStorage.get) match {
case UnknownTag => fail
case nextTag => replyResolved(nextTag)
case nextTag => replyResolved(nextTag)
}
}
}
Expand Down Expand Up @@ -67,7 +67,7 @@ class OfferServiceBot[F[_]: Concurrent](
private def withoutError(state: State): State =
state match {
case Error(returnTo, _) => returnTo
case _ => state
case _ => state
}

private def requestLogged(req: Either[EditMessageText, SendMessage]): F[Unit] =
Expand Down
3 changes: 2 additions & 1 deletion bot/src/main/scala/com/github/mmvpm/bot/model/Draft.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ case class Draft(
name: Option[String] = None,
price: Option[Long] = None,
description: Option[String] = None,
photos: Seq[String] = Seq.empty)
photos: Seq[String] = Seq.empty
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ trait Renderer {
def render(
state: State,
editMessage: Option[MessageID]
)(implicit message: Message
): Either[EditMessageText, SendMessage]
)(implicit message: Message): Either[EditMessageText, SendMessage]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RendererImpl extends Renderer {
override def render(
state: State,
editMessage: Option[MessageID] = None
)(implicit message: Message): Either[EditMessageText, SendMessage] = {
)(implicit message: Message): Either[EditMessageText, SendMessage] = {
val buttons = state.next.map { tag =>
InlineKeyboardButton.callbackData(buttonBy(tag), tag)
}
Expand All @@ -26,4 +26,3 @@ class RendererImpl extends Renderer {
Right(send)
}
}

50 changes: 25 additions & 25 deletions bot/src/main/scala/com/github/mmvpm/bot/state/State.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,37 @@ object State {

def buttonBy(tag: Tag): Button =
tag match {
case SearchTag => "Найти товар"
case ListingTag => "На следующую страницу"
case CreateOfferNameTag => "Разместить объявление"
case CreatedOfferTag => "Опубликовать объявление"
case MyOffersTag => "Посмотреть мои объявления"
case EditOfferTag => "Изменить это объявление"
case EditOfferNameTag => "Название"
case EditOfferPriceTag => "Цену"
case SearchTag => "Найти товар"
case ListingTag => "На следующую страницу"
case CreateOfferNameTag => "Разместить объявление"
case CreatedOfferTag => "Опубликовать объявление"
case MyOffersTag => "Посмотреть мои объявления"
case EditOfferTag => "Изменить это объявление"
case EditOfferNameTag => "Название"
case EditOfferPriceTag => "Цену"
case EditOfferDescriptionTag => "Описание"
case AddOfferPhotoTag => "Добавить фото"
case DeleteOfferPhotosTag => "Удалить все фото"
case DeletedOfferTag => "Удалить это объявление"
case BackTag => "Назад"
case StartedTag => "Вернуться в начало"
case UnknownTag => sys.error(s"buttonBy($tag)")
case AddOfferPhotoTag => "Добавить фото"
case DeleteOfferPhotosTag => "Удалить все фото"
case DeletedOfferTag => "Удалить это объявление"
case BackTag => "Назад"
case StartedTag => "Вернуться в начало"
case UnknownTag => sys.error(s"buttonBy($tag)")
}

def getNextStateTag(current: State): Tag =
current match {
case Search(_) => ListingTag
case Listing(_, _, _) => OneOfferTag
case MyOffers(_, _) => MyOfferTag
case CreateOfferName(_) => CreateOfferPriceTag
case CreateOfferPrice(_, _) => CreateOfferDescriptionTag
case Search(_) => ListingTag
case Listing(_, _, _) => OneOfferTag
case MyOffers(_, _) => MyOfferTag
case CreateOfferName(_) => CreateOfferPriceTag
case CreateOfferPrice(_, _) => CreateOfferDescriptionTag
case CreateOfferDescription(_, _) => CreateOfferPhotoTag
case CreateOfferPhoto(_, _) => CreateOfferPhotoTag // upload another photo
case EditOfferName(_) => UpdatedOfferTag
case EditOfferPrice(_) => UpdatedOfferTag
case EditOfferDescription(_) => UpdatedOfferTag
case AddOfferPhoto(_) => UpdatedOfferTag
case _ => UnknownTag
case CreateOfferPhoto(_, _) => CreateOfferPhotoTag // upload another photo
case EditOfferName(_) => UpdatedOfferTag
case EditOfferPrice(_) => UpdatedOfferTag
case EditOfferDescription(_) => UpdatedOfferTag
case AddOfferPhoto(_) => UpdatedOfferTag
case _ => UnknownTag
}

// previous state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ class StateManagerImpl[F[_]: Monad] extends StateManager[F] {

override def getNextState(tag: String, current: State)(implicit message: Message): F[State] =
tag match {
case SearchTag => toSearch(current)
case ListingTag => toListing(current)
case OneOfferTag => toOneOffer(current)
case CreateOfferNameTag => toCreateOfferName(current)
case CreateOfferPriceTag => toCreateOfferPrice(current)
case SearchTag => toSearch(current)
case ListingTag => toListing(current)
case OneOfferTag => toOneOffer(current)
case CreateOfferNameTag => toCreateOfferName(current)
case CreateOfferPriceTag => toCreateOfferPrice(current)
case CreateOfferDescriptionTag => toCreateOfferDescription(current)
case CreateOfferPhotoTag => toCreateOfferPhoto(current)
case CreatedOfferTag => toCreatedOffer(current)
case MyOffersTag => toMyOffers(current)
case MyOfferTag => toMyOffer(current)
case EditOfferTag => toEditOffer(current)
case EditOfferNameTag => toEditOfferName(current)
case EditOfferPriceTag => toEditOfferPrice(current)
case EditOfferDescriptionTag => toEditOfferDescription(current)
case AddOfferPhotoTag => toAddOfferPhoto(current)
case DeleteOfferPhotosTag => toDeleteOfferPhotos(current)
case UpdatedOfferTag => toUpdatedOffer(current)
case DeletedOfferTag => toDeleteOffer(current)
case BackTag => toBack(current)
case StartedTag => toStarted(current)
case CreateOfferPhotoTag => toCreateOfferPhoto(current)
case CreatedOfferTag => toCreatedOffer(current)
case MyOffersTag => toMyOffers(current)
case MyOfferTag => toMyOffer(current)
case EditOfferTag => toEditOffer(current)
case EditOfferNameTag => toEditOfferName(current)
case EditOfferPriceTag => toEditOfferPrice(current)
case EditOfferDescriptionTag => toEditOfferDescription(current)
case AddOfferPhotoTag => toAddOfferPhoto(current)
case DeleteOfferPhotosTag => toDeleteOfferPhotos(current)
case UpdatedOfferTag => toUpdatedOffer(current)
case DeletedOfferTag => toDeleteOffer(current)
case BackTag => toBack(current)
case StartedTag => toStarted(current)
}

// transitions
Expand All @@ -54,7 +54,7 @@ class StateManagerImpl[F[_]: Monad] extends StateManager[F] {
offerId <- message.text
offers <- current match {
case Listing(_, offers, _) => Some(offers)
case _ => None
case _ => None
}
offer <- offers.find(_.id.toString == offerId)
} yield OneOffer(current, offer)
Expand All @@ -68,7 +68,7 @@ class StateManagerImpl[F[_]: Monad] extends StateManager[F] {
private def toCreateOfferPrice(current: State)(implicit message: Message): F[State] =
message.text match {
case Some(name) => CreateOfferPrice(current, Draft(name = Some(name))).pure
case _ => Error(current, "Пожалуйста, введите название объявления").pure
case _ => Error(current, "Пожалуйста, введите название объявления").pure
}

private def toCreateOfferDescription(current: State)(implicit message: Message): F[State] = {
Expand All @@ -77,7 +77,7 @@ class StateManagerImpl[F[_]: Monad] extends StateManager[F] {
price <- priceRaw.toLongOption
draft <- current match {
case CreateOfferPrice(_, draft) => Some(draft)
case _ => None
case _ => None
}
updatedDraft = draft.copy(price = Some(price))
} yield CreateOfferDescription(current, updatedDraft)
Expand Down Expand Up @@ -111,7 +111,7 @@ class StateManagerImpl[F[_]: Monad] extends StateManager[F] {
private def toCreatedOffer(current: State)(implicit message: Message): F[State] =
current match {
case CreateOfferPhoto(_, draft) => CreatedOffer(current, draft).pure
case _ => Error(current, "Произошла ошибка! Попробуйте ещё раз").pure
case _ => Error(current, "Произошла ошибка! Попробуйте ещё раз").pure
}

private def toMyOffers(current: State)(implicit message: Message): F[State] =
Expand All @@ -127,14 +127,14 @@ class StateManagerImpl[F[_]: Monad] extends StateManager[F] {
offerId <- message.text
offers <- current match {
case MyOffers(_, offers) => Some(offers)
case _ => None
case _ => None
}
offer <- offers.find(_.id == UUID.fromString(offerId))
} yield offer

optOffer match {
case Some(offer) => MyOffer(current, offer).pure
case None => Error(current, "К сожалению, такого id не существует! Попробуйте ещё раз").pure
case None => Error(current, "К сожалению, такого id не существует! Попробуйте ещё раз").pure
}
}

Expand Down Expand Up @@ -201,7 +201,7 @@ class StateManagerImpl[F[_]: Monad] extends StateManager[F] {
// returns the nearest EditOffer
previous match {
case EditOffer(_) => previous.pure
case _ => previous.optPrevious.getOrElse(Started).pure
case _ => previous.optPrevious.getOrElse(Started).pure
}
case _ =>
current.optPrevious.getOrElse(Started).pure
Expand Down
Loading