Skip to content

Commit

Permalink
Merge pull request #57 from teknologi-umum/refactor/telebot-dependency
Browse files Browse the repository at this point in the history
refactor: make telebot dependency to support context
  • Loading branch information
elianiva authored Jan 6, 2024
2 parents 9b87dda + 440ca1a commit 4bcc511
Show file tree
Hide file tree
Showing 90 changed files with 10,836 additions and 561 deletions.
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* @aldy505 @elianiva
* @teknologi-umum/backend-go
.github @teknologi-umum/infrastructure
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Setup jq
uses: dcarbone/[email protected]

- name: Checkout code
uses: actions/checkout@v4
with:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Setup jq
uses: dcarbone/[email protected]

- name: Checkout code
uses: actions/checkout@v4
with:
Expand Down
1 change: 1 addition & 0 deletions .idea/vcs.xml
100755 → 100644

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package analytics
import (
"github.com/allegro/bigcache/v3"
"github.com/jmoiron/sqlx"
tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

// Dependency is the dependency injection struct
Expand Down
2 changes: 1 addition & 1 deletion analytics/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"

tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

// NewUser adds a newly joined user on the group into the database.
Expand Down
2 changes: 1 addition & 1 deletion analytics/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/getsentry/sentry-go"
tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

func TestNewUser(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion analytics/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

// NewMessage handles an incoming message from the group
Expand Down
2 changes: 1 addition & 1 deletion analytics/msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package analytics_test
import (
"testing"

tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

func TestNewMsg(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion analytics/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/teknologi-umum/captcha/utils"

tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

type NullInt64 sql.NullInt64
Expand Down
2 changes: 1 addition & 1 deletion analytics/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/teknologi-umum/captcha/analytics"

tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

func TestParseGroupMember(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions analytics/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/teknologi-umum/captcha/shared"
"github.com/teknologi-umum/captcha/utils"

tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

func (d *Dependency) SwarmLog(user *tb.User, groupID int64, finishedCaptcha bool) {
Expand Down Expand Up @@ -125,7 +125,7 @@ func (d *Dependency) UpdateSwarm(user *tb.User, groupID int64, finishedCaptcha b
}

func (d *Dependency) PurgeBots(ctx context.Context, m *tb.Message) {
admins, err := d.Bot.AdminsOf(m.Chat)
admins, err := d.Bot.AdminsOf(ctx, m.Chat)
if err != nil {
shared.HandleError(ctx, fmt.Errorf("get admins: %w", err))
return
Expand Down Expand Up @@ -186,7 +186,7 @@ func (d *Dependency) PurgeBots(ctx context.Context, m *tb.Message) {
return
}

err = d.Bot.Ban(m.Chat, &tb.ChatMember{
err = d.Bot.Ban(ctx, m.Chat, &tb.ChatMember{
RestrictedUntil: tb.Forever(),
User: &tb.User{
ID: userID,
Expand Down Expand Up @@ -237,7 +237,7 @@ func (d *Dependency) PurgeBots(ctx context.Context, m *tb.Message) {
return
}

_, err = d.Bot.Send(m.Chat, fmt.Sprintf("%d bots have been banned", len(userIDs)))
_, err = d.Bot.Send(ctx, m.Chat, fmt.Sprintf("%d bots have been banned", len(userIDs)))
if err != nil {
shared.HandleError(ctx, fmt.Errorf("send: %w", err))
return
Expand Down
4 changes: 3 additions & 1 deletion ascii/ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/teknologi-umum/captcha/shared"
"github.com/teknologi-umum/captcha/utils"

tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

// Dependencies contains dependency injection struct
Expand All @@ -25,13 +25,15 @@ func (d *Dependencies) Ascii(ctx context.Context, m *tb.Message) {
gen := utils.GenerateAscii(m.Payload)

_, err := d.Bot.Send(
ctx,
m.Chat,
"<pre>"+gen+"</pre>",
&tb.SendOptions{ParseMode: tb.ModeHTML, AllowWithoutReply: true},
)
if err != nil {
if errors.Is(err, tb.ErrEmptyMessage) {
_, err := d.Bot.Send(
ctx,
m.Chat,
"That text is not supported yet",
&tb.SendOptions{
Expand Down
2 changes: 1 addition & 1 deletion captcha/additional.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strconv"

tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

// Collect AdditionalMsg that was sent because the user did something
Expand Down
16 changes: 6 additions & 10 deletions captcha/answer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/allegro/bigcache/v3"
"github.com/pkg/errors"
tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

// WaitForAnswer is the handler for listening to incoming user message.
Expand Down Expand Up @@ -76,6 +76,7 @@ func (d *Dependencies) WaitForAnswer(ctx context.Context, m *tb.Message) {
if answer != captcha.Answer {
remainingTime := time.Until(captcha.Expiry)
wrongMsg, err := d.Bot.Send(
ctx,
m.Chat,
"Jawaban captcha salah, harap coba lagi. Kamu punya "+
strconv.Itoa(int(remainingTime.Seconds()))+
Expand All @@ -84,15 +85,10 @@ func (d *Dependencies) WaitForAnswer(ctx context.Context, m *tb.Message) {
ParseMode: tb.ModeHTML,
ReplyTo: m,
DisableWebPagePreview: true,
AllowWithoutReply: true,
},
)
if err != nil {
if strings.Contains(err.Error(), "replied message not found") {
// Don't retry to send the message if the user won't know
// which message we're replying to.
return
}

if strings.Contains(err.Error(), "retry after") {
// If this happens, probably we're in a spam bot surge and would
// probably don't care with the user captcha after all.
Expand Down Expand Up @@ -153,7 +149,7 @@ func (d *Dependencies) WaitForAnswer(ctx context.Context, m *tb.Message) {
if msgID == "" {
continue
}
err = d.deleteMessageBlocking(&tb.StoredMessage{
err = d.deleteMessageBlocking(ctx, &tb.StoredMessage{
ChatID: m.Chat.ID,
MessageID: msgID,
})
Expand All @@ -168,7 +164,7 @@ func (d *Dependencies) WaitForAnswer(ctx context.Context, m *tb.Message) {
if msgID == "" {
continue
}
err = d.deleteMessageBlocking(&tb.StoredMessage{
err = d.deleteMessageBlocking(ctx, &tb.StoredMessage{
ChatID: m.Chat.ID,
MessageID: msgID,
})
Expand All @@ -179,7 +175,7 @@ func (d *Dependencies) WaitForAnswer(ctx context.Context, m *tb.Message) {
}

// Delete the question message.
err = d.deleteMessageBlocking(&tb.StoredMessage{
err = d.deleteMessageBlocking(ctx, &tb.StoredMessage{
ChatID: m.Chat.ID,
MessageID: captcha.QuestionID,
})
Expand Down
2 changes: 1 addition & 1 deletion captcha/captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package captcha

import (
"github.com/allegro/bigcache/v3"
tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

// Dependencies contains the dependency injection struct for
Expand Down
36 changes: 15 additions & 21 deletions captcha/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,30 @@ package captcha

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/teknologi-umum/captcha/shared"

tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

// deleteMessage creates a timer of one minute to delete a certain message.
func (d *Dependencies) deleteMessage(ctx context.Context, message *tb.StoredMessage) {
c := make(chan struct{}, 1)
time.AfterFunc(time.Minute*1, func() {
for {
err := d.Bot.Delete(message)
err := d.Bot.Delete(ctx, message)
if err != nil && !strings.Contains(err.Error(), "message to delete not found") {
if strings.Contains(err.Error(), "retry after") {
// Acquire the retry number
retry, err := strconv.Atoi(strings.Split(strings.Split(err.Error(), "telegram: retry after ")[1], " ")[0])
if err != nil {
// If there's an error, we'll just retry after 15 second
retry = 15
var floodError tb.FloodError
if errors.As(err, &floodError) {
if floodError.RetryAfter == 0 {
floodError.RetryAfter = 15
}

// Let's wait a bit and retry
time.Sleep(time.Second * time.Duration(retry))
time.Sleep(time.Second * time.Duration(floodError.RetryAfter))
continue
}

Expand All @@ -49,20 +46,17 @@ func (d *Dependencies) deleteMessage(ctx context.Context, message *tb.StoredMess
<-c
}

func (d *Dependencies) deleteMessageBlocking(message *tb.StoredMessage) error {
func (d *Dependencies) deleteMessageBlocking(ctx context.Context, message *tb.StoredMessage) error {
for {
err := d.Bot.Delete(message)
err := d.Bot.Delete(ctx, message)
if err != nil && !strings.Contains(err.Error(), "message to delete not found") {
if strings.Contains(err.Error(), "retry after") {
// Acquire the retry number
retry, err := strconv.Atoi(strings.Split(strings.Split(err.Error(), "telegram: retry after ")[1], " ")[0])
if err != nil {
// If there's an error, we'll just retry after 15 second
retry = 15
var floodError tb.FloodError
if errors.As(err, &floodError) {
if floodError.RetryAfter == 0 {
floodError.RetryAfter = 15
}

// Let's wait a bit and retry
time.Sleep(time.Second * time.Duration(retry))
time.Sleep(time.Second * time.Duration(floodError.RetryAfter))
continue
}

Expand Down
36 changes: 9 additions & 27 deletions captcha/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

"github.com/allegro/bigcache/v3"

tb "gopkg.in/telebot.v3"
tb "github.com/teknologi-umum/captcha/internal/telebot"
)

// Captcha struct keeps all the data needed for the captcha
Expand Down Expand Up @@ -72,7 +72,7 @@ func (d *Dependencies) CaptchaUserJoin(ctx context.Context, m *tb.Message) {
if err != nil {
if errors.Is(err, bigcache.ErrEntryNotFound) {
// Find and set
admins, err = d.Bot.AdminsOf(m.Chat)
admins, err = d.Bot.AdminsOf(ctx, m.Chat)
if err != nil {
if !strings.Contains(err.Error(), "Gateway Timeout (504)") && !strings.Contains(err.Error(), "retry after") {
shared.HandleBotError(ctx, err, d.Bot, m)
Expand Down Expand Up @@ -138,25 +138,24 @@ func (d *Dependencies) CaptchaUserJoin(ctx context.Context, m *tb.Message) {
SENDMSG_RETRY:
// Send the question first.
msgQuestion, err := d.Bot.Send(
ctx,
m.Chat,
question,
&tb.SendOptions{
ParseMode: tb.ModeHTML,
ReplyTo: m,
DisableWebPagePreview: true,
AllowWithoutReply: true,
},
)
if err != nil {
if strings.Contains(err.Error(), "retry after") {
// Acquire the retry number
retry, err := strconv.Atoi(strings.Split(strings.Split(err.Error(), "telegram: retry after ")[1], " ")[0])
if err != nil {
// If there's an error, we'll just retry after 10 second
retry = 10
var floodError tb.FloodError
if errors.As(err, &floodError) {
if floodError.RetryAfter == 0 {
floodError.RetryAfter = 15
}

// Let's wait a bit and retry
time.Sleep(time.Second * time.Duration(retry))
time.Sleep(time.Second * time.Duration(floodError.RetryAfter))
goto SENDMSG_RETRY
}

Expand All @@ -165,23 +164,6 @@ SENDMSG_RETRY:
goto SENDMSG_RETRY
}

if strings.Contains(err.Error(), "replied message not found") {
msgQuestion, err = d.Bot.Send(
m.Chat,
question,
&tb.SendOptions{
ParseMode: tb.ModeHTML,
DisableWebPagePreview: true,
},
)
if err != nil {
if !strings.Contains(err.Error(), "retry after") && !strings.Contains(err.Error(), "Gateway Timeout (504)") {
shared.HandleBotError(ctx, err, d.Bot, m)
return
}
}
}

// err could possibly be nil at this point, so we better check it out.
if err != nil {
shared.HandleBotError(ctx, err, d.Bot, m)
Expand Down
Loading

0 comments on commit 4bcc511

Please sign in to comment.