From f978d982522f0cb15fb5bb4b7bcb7659f3fca0ee Mon Sep 17 00:00:00 2001 From: lngohumble Date: Mon, 27 Dec 2021 11:58:19 +0100 Subject: [PATCH] volcano initial commit --- internal/runtime/function.go | 15 +++++++++- internal/telegram/faucet.go | 2 +- internal/telegram/handler.go | 47 +++++++++++++++++++++++++++++++ internal/telegram/inline_query.go | 3 ++ 4 files changed, 65 insertions(+), 2 deletions(-) diff --git a/internal/runtime/function.go b/internal/runtime/function.go index 96bbbc8..ee15f24 100644 --- a/internal/runtime/function.go +++ b/internal/runtime/function.go @@ -2,6 +2,7 @@ package runtime import ( cmap "github.com/orcaman/concurrent-map" + "sync" "time" ) @@ -20,6 +21,8 @@ type ResettableFunctionTicker struct { duration time.Duration Started bool name string + Quit chan struct{} + Wg sync.WaitGroup } type ResettableFunctionTickerOption func(*ResettableFunctionTicker) @@ -28,11 +31,11 @@ func WithDuration(d time.Duration) ResettableFunctionTickerOption { a.duration = d } } + func RemoveTicker(name string) { tickerMap.Remove(name) } func GetTicker(name string, option ...ResettableFunctionTickerOption) *ResettableFunctionTicker { - if t, ok := tickerMap.Get(name); ok { return t.(*ResettableFunctionTicker) } else { @@ -44,6 +47,8 @@ func GetTicker(name string, option ...ResettableFunctionTickerOption) *Resettabl func NewResettableFunctionTicker(name string, option ...ResettableFunctionTickerOption) *ResettableFunctionTicker { t := &ResettableFunctionTicker{ ResetChan: make(chan struct{}, 1), + Quit: make(chan struct{}, 1), + Wg: sync.WaitGroup{}, name: name, } @@ -57,12 +62,20 @@ func NewResettableFunctionTicker(name string, option ...ResettableFunctionTicker return t } +func (t *ResettableFunctionTicker) DoWait(f func()) { + t.Do(f) + t.Wg.Wait() +} func (t *ResettableFunctionTicker) Do(f func()) { t.Started = true tickerMap.Set(t.name, t) + t.Wg.Add(1) go func() { + defer t.Wg.Done() for { select { + case <-t.Quit: + return case <-t.Ticker.C: // ticker delivered signal. do function f f() diff --git a/internal/telegram/faucet.go b/internal/telegram/faucet.go index 72a2ce7..32a9f4b 100644 --- a/internal/telegram/faucet.go +++ b/internal/telegram/faucet.go @@ -26,7 +26,7 @@ import ( var ( inlineFaucetMenu = &tb.ReplyMarkup{ResizeReplyKeyboard: true} btnCancelInlineFaucet = inlineFaucetMenu.Data("🚫 Cancel", "cancel_faucet_inline") - btnAcceptInlineFaucet = inlineFaucetMenu.Data("✅ Collect", "confirm_faucet_inline") + btnAcceptInlineFaucet = inlineFaucetMenu.Data("✅ Start", "confirm_faucet_inline") ) type InlineFaucet struct { diff --git a/internal/telegram/handler.go b/internal/telegram/handler.go index d36279e..cfcc94d 100644 --- a/internal/telegram/handler.go +++ b/internal/telegram/handler.go @@ -248,6 +248,22 @@ func (bot TipBot) getHandler() []Handler { }, }, }, + { + Endpoints: []interface{}{"/volcano"}, + Handler: bot.volcanoHandler, + Interceptor: &Interceptor{ + Type: MessageInterceptor, + Before: []intercept.Func{ + bot.localizerInterceptor, + bot.logMessageInterceptor, + bot.requireUserInterceptor, + bot.lockInterceptor, + }, + OnDefer: []intercept.Func{ + bot.unlockInterceptor, + }, + }, + }, { Endpoints: []interface{}{"/tipjar", "/spendendose"}, Handler: bot.tipjarHandler, @@ -543,6 +559,37 @@ func (bot TipBot) getHandler() []Handler { }, }, }, + { + Endpoints: []interface{}{&btnAcceptInlineVolcano}, + Handler: bot.acceptInlineVolcanoHandler, + Interceptor: &Interceptor{ + Type: CallbackInterceptor, + Before: []intercept.Func{ + //bot.singletonCallbackInterceptor, + bot.localizerInterceptor, + bot.loadUserInterceptor, + bot.lockInterceptor, + }, + OnDefer: []intercept.Func{ + bot.unlockInterceptor, + }, + }, + }, + { + Endpoints: []interface{}{&btnCancelInlineVolcano}, + Handler: bot.cancelInlineVolcanoHandler, + Interceptor: &Interceptor{ + Type: CallbackInterceptor, + Before: []intercept.Func{ + bot.localizerInterceptor, + bot.requireUserInterceptor, + bot.lockInterceptor, + }, + OnDefer: []intercept.Func{ + bot.unlockInterceptor, + }, + }, + }, { Endpoints: []interface{}{&btnAcceptInlineFaucet}, Handler: bot.acceptInlineFaucetHandler, diff --git a/internal/telegram/inline_query.go b/internal/telegram/inline_query.go index 32d4596..cdf2bc6 100644 --- a/internal/telegram/inline_query.go +++ b/internal/telegram/inline_query.go @@ -146,6 +146,9 @@ func (bot TipBot) anyQueryHandler(ctx context.Context, q *tb.Query) { if strings.HasPrefix(q.Text, "send") || strings.HasPrefix(q.Text, "pay") { bot.handleInlineSendQuery(ctx, q) } + if strings.HasPrefix(q.Text, "volcano") { + //bot.handleInlineVolcanoQuery(ctx, q) + } if strings.HasPrefix(q.Text, "faucet") || strings.HasPrefix(q.Text, "zapfhahn") || strings.HasPrefix(q.Text, "kraan") || strings.HasPrefix(q.Text, "grifo") { if len(strings.Split(q.Text, " ")) > 1 {