func (h *Handler) Serve(ctrl *telegram.Control, bot *tgbotapi.BotAPI, update tgbotapi.Update) { var call slugger.Call if v := ctrl.Context().Value(reflect.TypeOf(call)); v == nil { ctrl.Throw(errors.New("slugger should be used before")) return } else { call = v.(slugger.Call) } if len(call.Query) > 0 { switch len(call.Query) { case 1: h.roubleInline(ctrl, bot, update, call) default: h.rateInline(ctrl, bot, update, call) } } else { switch call.Args[0] { case "rate": h.rate(ctrl, bot, update, call) default: h.rouble(ctrl, bot, update, call) } } }
func (h *Handler) rateInline(ctrl *telegram.Control, bot *tgbotapi.BotAPI, update tgbotapi.Update, call slugger.Call) { if len(call.Query) != 2 { ctrl.Throw(ErrRateExpectTwoArguments) return } if err := validateCurrency(call.Query[0]); err != nil { ctrl.Throw(err) return } if err := validateCurrency(call.Query[1]); err != nil { ctrl.Throw(err) return } from, to := toCurrency(call.Query[0]), toCurrency(call.Query[1]) rate, err := h.f.GetRate(ctrl.Context(), from, to) if err != nil { ctrl.Throw(err) return } bot.AnswerInlineQuery(tgbotapi.InlineConfig{ InlineQueryID: update.InlineQuery.ID, Results: []interface{}{ inlineResultArticle(from, to, rate.Rate), }, }) ctrl.Next() }
func (h *Handler) rouble(ctrl *telegram.Control, bot *tgbotapi.BotAPI, update tgbotapi.Update, call slugger.Call) { if len(call.Args) != 1 { ctrl.Throw(ErrRoubleExpectOneArgument) return } if err := validateCurrency(call.Args[0]); err != nil { ctrl.Throw(err) return } rate, err := h.f.GetRate(ctrl.Context(), toCurrency(call.Args[0]), finance.RUB) if err != nil { ctrl.Throw(err) return } bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, strconv.FormatFloat(rate.Rate, 'f', 2, 64))) ctrl.Next() }