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) roubleInline(ctrl *telegram.Control, bot *tgbotapi.BotAPI, update tgbotapi.Update, call slugger.Call) { if len(call.Query) != 1 { ctrl.Throw(ErrRoubleExpectOneArgument) return } if err := validateCurrency(call.Query[0]); err != nil { ctrl.Throw(err) return } from := toCurrency(call.Query[0]) rate, err := h.f.GetRate(ctrl.Context(), from, finance.RUB) if err != nil { ctrl.Throw(err) return } bot.AnswerInlineQuery(tgbotapi.InlineConfig{ InlineQueryID: update.InlineQuery.ID, Results: []interface{}{ inlineResultArticle(from, finance.RUB, rate.Rate), }, }) ctrl.Next() }
func (self Condition) Serve(ctrl *telegram.Control, bot *tgbotapi.BotAPI, update tgbotapi.Update) { if self.Matcher.Match(update) { self.Handler.Serve(ctrl, bot, update) return } ctrl.Next() }
func (r *Slugger) Serve(ctrl *telegram.Control, bot *tgbotapi.BotAPI, update tgbotapi.Update) { var call Call text := strings.TrimPrefix(update.Message.Text, methodPrefix) fill(&call.Args, text) fill(&call.Query, update.InlineQuery.Query) ctrl.NextWithValue(reflect.TypeOf(call), call) 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() }
func (h *Handler) Serve(ctrl *telegram.Control, bot *tgbotapi.BotAPI, update tgbotapi.Update) { msg := tgbotapi.NewMessage(update.Message.Chat.ID, help.HelpContents) msg.ParseMode = telegram.ParseModeMarkdown bot.Send(msg) ctrl.Next() }
func (c *Canceler) Serve(ctrl *telegram.Control, bot *tgbotapi.BotAPI, update tgbotapi.Update) { ctrl.NextWithTimeout(c.Timeout) ctrl.Next() }