Documentation ¶
Index ¶
- Constants
- Variables
- type Captcha
- type Dependencies
- func (d *Dependencies) CaptchaUserJoin(ctx context.Context, m *tb.Message)
- func (d *Dependencies) CaptchaUserLeave(ctx context.Context, m *tb.Message)
- func (d *Dependencies) Cleanup()
- func (d *Dependencies) NonTextListener(ctx context.Context, m *tb.Message)
- func (d *Dependencies) WaitForAnswer(ctx context.Context, m *tb.Message)
Constants ¶
const ( // BanDuration specifies how long a user will be banned in the group. BanDuration = 60 * time.Second // Timeout specifies how long the captcha question will be valid. // After this time, the user will be kicked. // Or banned exactly, for one hour. Timeout = 61 * time.Second )
Variables ¶
var DefaultQuestion = "Halo, {user}!\n\n" +
"Sebelum lanjut, selesaikan captcha ini dulu agar bisa chat di grup ini. Ubah teks besar yang kamu lihat dibawah pesan ini menjadi teks biasa. Teks tersebut hanya berupa kombinasi angka 1-9 dengan huruf V, W, X, dan Y, jangan salah ketik ya!\n\n" +
"Ini teksnya 👇, kamu punya waktu 1 menit dari sekarang! Kalau tulisannya pecah, dirotate layarnya kebentuk landscape ya.\n\n" +
"<pre>{captcha}</pre>"
DefaultQuestion contains the default captcha questions.
Functions ¶
This section is empty.
Types ¶
type Captcha ¶
type Captcha struct { // Store the correct answer for the captcha Answer string `json:"a"` // Expiry time for the captcha Expiry time.Time `json:"e"` ChatID int64 `json:"c"` SenderID int64 `json:"s"` QuestionID string `json:"q"` AdditionalMessages []string `json:"am"` UserMessages []string `json:"um"` }
Captcha struct keeps all the data needed for the captcha for a certain user.
It will be converted to JSON format (as array of bytes or []byte) and then will be stored to the in memory cache, with the key of the corresponding Telegram User ID.
type Dependencies ¶
type Dependencies struct { DB *badger.DB Memory *bigcache.BigCache Bot *tb.Bot TeknumGroupID int64 }
Dependencies contains the dependency injection struct for methods in the captcha package.
func (*Dependencies) CaptchaUserJoin ¶
func (d *Dependencies) CaptchaUserJoin(ctx context.Context, m *tb.Message)
CaptchaUserJoin is the most frustrating function that I've written at this point of time.
As the function name says, it will prompt a captcha to the incoming user that has just joined the group.
At the end of the function, it will create 2 goroutines in which both of them are responsible for kicking the user out of the group.
func (*Dependencies) CaptchaUserLeave ¶
func (d *Dependencies) CaptchaUserLeave(ctx context.Context, m *tb.Message)
CaptchaUserLeave handles the event when a user left the group. This will check if the user is in the memory of current active captcha or not.
If it is, the captcha will be deleted.
func (*Dependencies) Cleanup ¶
func (d *Dependencies) Cleanup()
Cleanup will iterate over every keys and make sure the expiry has not been exceeded by 10 seconds. If it is, we'll kick the person.
func (*Dependencies) NonTextListener ¶
func (d *Dependencies) NonTextListener(ctx context.Context, m *tb.Message)
NonTextListener is the handler for every incoming payload that is not a text format.
func (*Dependencies) WaitForAnswer ¶
func (d *Dependencies) WaitForAnswer(ctx context.Context, m *tb.Message)
WaitForAnswer is the handler for listening to incoming user message. It will uh... do a pretty long task of validating the input message.