Example #1
0
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)
		}
	}

}
Example #2
0
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()
}
Example #3
0
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()
}
Example #4
0
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()
}
Example #5
0
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()
}
Example #6
0
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()
}
Example #7
0
func (c *Canceler) Serve(ctrl *telegram.Control, bot *tgbotapi.BotAPI, update tgbotapi.Update) {
	ctrl.NextWithTimeout(c.Timeout)
	ctrl.Next()
}