• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Civil / tg-simple-regex-antispam / 13070263183

31 Jan 2025 09:45AM UTC coverage: 0.0%. Remained the same
13070263183

Pull #5

github

Civil
Refactor and improve bot

 * Fixed small bug in forward action
 * package statefulFilters renamed to chains
 * generic badgerdb interface
 * add filter that counts repeated messages
 * add filter that checks if message is a story
 * add filter that checks if message was marked as spam
Pull Request #5: some improvements

0 of 1422 new or added lines in 20 files covered. (0.0%)

21 existing lines in 3 files now uncovered.

0 of 3756 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/actions/addReportButton/action.go
1
package addReportButton
2

3
import (
4
        "bytes"
5
        "errors"
6
        "fmt"
7
        "strings"
8
        "time"
9

10
        "github.com/mymmrac/telego"
11
        "go.uber.org/zap"
12

13
        "github.com/Civil/tg-simple-regex-antispam/actions/interfaces"
14
        interfaces2 "github.com/Civil/tg-simple-regex-antispam/filters/interfaces"
15
        "github.com/Civil/tg-simple-regex-antispam/filters/types/scoringResult"
16
        config2 "github.com/Civil/tg-simple-regex-antispam/helper/config"
17
)
18

19
type Action struct {
20
        logger *zap.Logger
21
        bot    *telego.Bot
22

23
        isAnonymousReport bool
24
        msgPrefix         string
25
}
26

27
var ErrNotSupported = errors.New("not supported")
28

29
func (r *Action) Apply(_ interfaces2.StatefulFilter, _ *scoringResult.ScoringResult, _ telego.ChatID, _ []int64,
NEW
30
        _ int64, _ any) error {
×
31
        return ErrNotSupported
×
32
}
×
33

34
func (r *Action) ApplyToMessage(_ interfaces2.StatefulFilter, _ *scoringResult.ScoringResult,
NEW
35
        message *telego.Message, extraParams any) error {
×
NEW
36
        vacationAdmins, ok := extraParams.(map[string]time.Time)
×
NEW
37
        if !ok {
×
NEW
38
                r.logger.Error("extraParams is not a map[string]time.Time")
×
NEW
39
                vacationAdmins = make(map[string]time.Time)
×
NEW
40
        }
×
41
        r.logger.Debug("adding report button to message",
×
42
                zap.Any("message", message),
×
43
                zap.Any("chat", message.Chat),
×
44
        )
×
45
        params := &telego.GetChatAdministratorsParams{
×
46
                ChatID: message.Chat.ChatID(),
×
47
        }
×
48
        admins, err := r.bot.GetChatAdministrators(params)
×
49
        if err != nil {
×
50
                return fmt.Errorf("getting chat administrators: %w", err)
×
51
        }
×
52
        msgBuf := bytes.NewBuffer([]byte{})
×
53
        if r.msgPrefix != "" {
×
54
                msgBuf.WriteString(r.msgPrefix + " ")
×
55
        }
×
56
        if r.isAnonymousReport {
×
NEW
57
                msgBuf.WriteString("Spam or chat rules violation: ")
×
58
        } else {
×
NEW
59
                msgBuf.WriteString("User @" + message.From.Username + " reported a spam or rules violation: ")
×
60
        }
×
61
        firstAdmin := true
×
62
        for _, admin := range admins {
×
63
                adminUsername := admin.MemberUser().Username
×
NEW
64
                if _, ok = vacationAdmins[adminUsername]; ok {
×
NEW
65
                        continue
×
66
                }
67
                if strings.HasSuffix(strings.ToLower(adminUsername), "bot") {
×
68
                        continue
×
69
                }
70
                if !firstAdmin {
×
71
                        msgBuf.WriteString(", ")
×
72
                }
×
73
                msgBuf.WriteRune('@')
×
74
                msgBuf.WriteString(adminUsername)
×
75
                firstAdmin = false
×
76
        }
77

78
        sendMessageParams := &telego.SendMessageParams{
×
79
                ChatID: message.Chat.ChatID(),
×
80
                Text:   msgBuf.String(),
×
81
                ReplyParameters: &telego.ReplyParameters{
×
82
                        MessageID: message.MessageID,
×
83
                },
×
84
        }
×
85

×
86
        _, err = r.bot.SendMessage(
×
87
                sendMessageParams,
×
88
        )
×
89
        msgBuf.Reset()
×
90
        if err != nil {
×
91
                return err
×
92
        }
×
93

94
        return nil
×
95
}
96

97
func (r *Action) GetName() string {
×
98
        return "addReportButton"
×
99
}
×
100

101
func (r *Action) PerMessage() bool {
×
102
        return true
×
103
}
×
104

105
func New(logger *zap.Logger, bot *telego.Bot, config map[string]any) (interfaces.Action, error) {
×
106
        anonymousReport, err := config2.GetOptionBoolWithDefault(config, "isAnonymousReport", true)
×
107
        if err != nil {
×
108
                return nil, err
×
109
        }
×
110
        msgPrefix := config2.GetOptionStringWithDefault(config, "msgPrefix", "")
×
111
        return &Action{
×
112
                logger: logger,
×
113
                bot:    bot,
×
114

×
115
                isAnonymousReport: anonymousReport,
×
116
                msgPrefix:         msgPrefix,
×
117
        }, nil
×
118
}
119

120
func Help() string {
×
121
        return "deleteAndBan doesn't require any parameter"
×
122
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc