bot

package
v0.0.0-...-397a81d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2020 License: GPL-2.0 Imports: 19 Imported by: 0

Documentation

Overview

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

SPDX-License-Identifier: GPL-2.0

Index

Constants

View Source
const (
	FLOOD_TIME_INTERVAL     = 10
	FLOOD_MAX_ALLOWED_MSGS  = 3
	FLOOD_MAX_ALLOWED_WORDS = 500
	CONT_MSGS_ALLOWED       = 20
	CONT_USER_MSG_ALLOWED   = 3
)
View Source
const (
	BDFL                     = "novitoll"
	TELEGRAM_BOT_USERNAME    = "novitoll_daemon_bot"
	TIME_TO_DELETE_REPLY_MSG = 10
	// 7 days - 1 sec
	EVERY_LAST_SEC_7TH_DAY = 604799
	// 15 min
	NEWCOMER_URL_POST_DELAY = 900
)

Variables

View Source
var (
	GET               = "GET"
	POST              = "POST"
	TELEGRAM_URL      = "https://api.telegram.org/bot%s/%s"
	TELEGRAM_TOKEN    = "123"
	ParseModeMarkdown = "Markdown"
	ParseModeHTML     = "HTML"
)
View Source
var (
	// TODO(novitoll): Move this to configuration file
	AD_WORDS = []string{"t.me/", "t.cn/", "joinchat"}
	// TODO(novitoll): Make a channel dependent
	EXCEPTIONS = []string{"t.me/proxy", "t.me/cyberseckz"}
)
View Source
var (
	// All these REDIS_* are Redis keys with following common
	// nested map:
	// { <chat-id>: { <user_id>: timestamp } }
	REDIS_USER_PENDING       = "NewComersAuthPending"
	REDIS_USER_VERIFIED      = "NewComersAuthVerified"
	REDIS_USER_KICKED        = "NewComersAuthKicked"
	REDIS_USER_LEFT          = "ParticipantLeft"
	REDIS_USER_PREV_LEFT     = "ParticipantLeftPrev"
	REDIS_USER_PREV_KICK     = "NewComersAuthKickedPrev"
	REDIS_USER_PREV_VERIFIED = "NewComersAuthVerifiedPrev"
	REDIS_USER_SENT_URL      = "UserSentUrl"
)
View Source
var (
	// This global variable is referenced in cronjobs.go
	ChatIds = make(map[int]time.Time)
)
View Source
var (

	// we could store this map in Redis as well,
	// but once we have the record here, we have to
	// check Redis (open TCP connection) per each message
	// because we don't know beforehand if the message is
	// from the Auth pending user or not. So fuck it
	NewComersAuthPending = make(map[int]string)
)
View Source
var (
	SENTIMENTS = []string{")", ""}
)
View Source
var (
	// Map to store user message statistics per chat_id
	// Data in the map is cleaned up when the CronJob executes
	// (every last second of 7th day)
	UserStatistics = make(map[int]map[int]*UserMessageStats)
)

Functions

func CronGetChatAdmins

func CronGetChatAdmins(j *Job) (interface{}, error)

func FanOutProcessJobs

func FanOutProcessJobs(job *Job, jobsFn []ProcessJobFn) (
	[]interface{}, []error)

func JobAdDetector

func JobAdDetector(j *Job) (interface{}, error)

func JobLeftParticipantDetector

func JobLeftParticipantDetector(j *Job) (interface{}, error)

func JobMsgStats

func JobMsgStats(j *Job) (interface{}, error)

func JobNewChatMemberAuth

func JobNewChatMemberAuth(j *Job) (interface{}, error)

func JobNewChatMemberDetector

func JobNewChatMemberDetector(j *Job) (interface{}, error)

func JobSentimentDetector

func JobSentimentDetector(j *Job) (interface{}, error)

func JobUrlDuplicationDetector

func JobUrlDuplicationDetector(j *Job) (interface{}, error)

Types

type App

type App struct {
	Features   *cfg.FeaturesCfg
	Lang       string
	Logger     *logrus.Logger
	ChatAdmins map[int][]string
}

func (*App) FlushQueueHandler

func (app *App) FlushQueueHandler(w http.ResponseWriter, r *http.Request)

func (*App) ProcessHandler

func (app *App) ProcessHandler(w http.ResponseWriter, r *http.Request)

Receives HTTP requests on /process end-point from Telegram and after parsing request body raw bytes, "BotInReq" struct is created which contains sufficient info about Telegram Chat, User, Message. After struct's creation, Process() goroutine is scheduled and the HTTP request handler is completed with 200/OK response.

func (*App) RegisterHandlers

func (app *App) RegisterHandlers()

func (*App) SendToTelegram

func (app *App) SendToTelegram(body interface{}, method string) (*BotInReqMsg, error)

type AppError

type AppError struct {
	// contains filtered or unexported fields
}

func (*AppError) Error

func (re *AppError) Error() string

type BotAnswerCallbackQuery

type BotAnswerCallbackQuery struct {
	CallbackQueryId string `json:"callback_query_id"`
}

https://core.telegram.org/bots/api#answercallbackquery

func (*BotAnswerCallbackQuery) AnswerCallbackQuery

func (body *BotAnswerCallbackQuery) AnswerCallbackQuery(app *App) (*BotInReqMsg, error)

type BotDeleteMsg

type BotDeleteMsg struct {
	ChatId    int `json:"chat_id"`
	MessageId int `json:"message_id"`
}

https://core.telegram.org/bots/api#deletemessage

func (*BotDeleteMsg) DeleteMsg

func (body *BotDeleteMsg) DeleteMsg(app *App) (*BotInReqMsg, error)

type BotForceReply

type BotForceReply struct {
	ForceReply bool `json:"force_reply"`
	Selective  bool `json:"selective"`
}

https://core.telegram.org/bots/api#forcereply

type BotGetAdmins

type BotGetAdmins struct {
	ChatId int `json:"chat_id"`
}

https://core.telegram.org/bots/api#getchatadministrators

func (*BotGetAdmins) GetAdmins

func (body *BotGetAdmins) GetAdmins(app *App) ([]*BotInReqMsg, error)

This is the same wrapper as above struct functions, but its response and request parsing are different, so have to duplicate the code without ready func. wrappers

type BotInReq

type BotInReq struct {
	Update_Id     int `json:"update_id"`
	Message       BotInReqMsg
	CallbackQuery CallbackQuery `json:"callback_query"`
}

func (*BotInReq) CronSchedule

func (req *BotInReq) CronSchedule(app *App)

func (*BotInReq) Process

func (req *BotInReq) Process(app *App)

type BotInReqMsg

type BotInReqMsg struct {
	From                User
	Text                string `json:"text"`
	Entities            []Message
	Date                int64   `json:"date"` // time.Unix()
	MessageId           int     `json:"message_id"`
	Chat                Chat    `json:"chat"`
	NewChatMembers      []User  `json:"new_chat_members"`
	NewChatMember       User    `json:"new_chat_member"`
	NewChatParticipant  User    `json:"new_chat_participant"`
	LeftChatParticipant User    `json:"left_chat_participant"`
	Sticker             Sticker `json:"sticker"`
	Caption             string  `json:"caption"`
}

type BotInResp

type BotInResp struct {
	Ok          bool        `json:"ok"`
	Result      BotInReqMsg `json:"result"`
	ErrorCode   int         `json:"error_code"`
	Description string      `json:"description"`
}

type BotInResp2

type BotInResp2 struct {
	Ok     bool `json:"ok"`
	Result bool `json:"result"`
}

type BotInRespMult

type BotInRespMult struct {
	Ok          bool           `json:"ok"`
	Result      []*BotInReqMsg `json:"result"`
	ErrorCode   int            `json:"error_code"`
	Description string         `json:"description"`
}

type BotKickChatMember

type BotKickChatMember struct {
	ChatId    int   `json:"chat_id"`
	UserId    int   `json:"user_id"`
	UntilDate int64 `json:"until_date"`
}

https://core.telegram.org/bots/api#kickchatmember

func (*BotKickChatMember) KickChatMember

func (body *BotKickChatMember) KickChatMember(app *App) (*BotInReqMsg, error)

type BotSendMsg

type BotSendMsg struct {
	ChatId           int         `json:"chat_id"`
	Text             string      `json:"text"`
	ParseMode        string      `json:"parse_mode"`
	ReplyToMessageId int         `json:"reply_to_message_id"`
	ReplyMarkup      interface{} `json:"reply_markup"`
}

https://core.telegram.org/bots/api#sendmessage

func (*BotSendMsg) SendMsg

func (body *BotSendMsg) SendMsg(app *App) (*BotInReqMsg, error)

type CallbackQuery

type CallbackQuery struct {
	Id              string      `json:"id"`
	InlineMessageId string      `json:"inline_message_id"`
	From            User        `json:"from"`
	Message         BotInReqMsg `json:"message"`
	ChatInstance    string      `json:"chat_instance"`
	Data            string      `json:"data"`
}

https://core.telegram.org/bots/api#callbackquery

type Chat

type Chat struct {
	Username   string `json:"username"`
	First_Name string `json:"first_name"`
	Type       string `json:"type"`
	Id         int    `json:"id"`
	Title      string `json:"title"`
}

type InlineKeyboardButton

type InlineKeyboardButton struct {
	Text         string `json:"text"`
	CallbackData string `json:"callback_data"`
}

https://core.telegram.org/bots/api#inlinekeyboardbutton

type InlineKeyboardMarkup

type InlineKeyboardMarkup struct {
	InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
}

https://core.telegram.org/bots/api#inlinekeyboardmarkup

type Job

type Job struct {
	// contains filtered or unexported fields
}

func (*Job) DeleteMessage

func (j *Job) DeleteMessage(resp *BotInReqMsg) (interface{}, error)

func (*Job) GetBatchFromRedis

func (j *Job) GetBatchFromRedis(redisConn *redis.Client, k string, limit int) interface{}

func (*Job) HasMessageContent

func (j *Job) HasMessageContent() bool

func (*Job) KickChatMember

func (j *Job) KickChatMember(userId int, username string) (interface{}, error)

func (*Job) SaveInRedis

func (j *Job) SaveInRedis(redisConn *redis.Client, k string, v interface{}, t int)

func (*Job) SendMessage

func (j *Job) SendMessage(replyText string, replyMsgId int) (*BotInReqMsg, error)

func (*Job) SendMessageWCleanup

func (j *Job) SendMessageWCleanup(text string, delay uint8, reply interface{}) (interface{}, error)

func (*Job) SendMessageWReply

func (j *Job) SendMessageWReply(replyText string, replyMsgId int, reply interface{}) (*BotInReqMsg, error)

type KeyboardBtn

type KeyboardBtn struct {
	Text string `json:"text"`
}

https://core.telegram.org/bots/api#keyboardbutton

type Message

type Message struct {
	Type          string `json:"type"`
	Length        int    `json:"length"`
	Url           string `json:"url"`
	MentionedUser User   `json:"user"`
}

https://core.telegram.org/bots/api#messageentity

type ProcessJobFn

type ProcessJobFn func(job *Job) (interface{}, error)

type ReplyKeyboardMarkup

type ReplyKeyboardMarkup struct {
	Keyboard        [][]KeyboardBtn `json:"keyboard"`
	OneTimeKeyboard bool            `json:"one_time_keyboard"`
	Selective       bool            `json:"selective"`
}

https://core.telegram.org/bots/api#replykeyboardmarkup

type Sticker

type Sticker struct {
	FileId string `json:"file_id"`
}

https://core.telegram.org/bots/api#sticker

type User

type User struct {
	Id           int    `json:"id"`
	Username     string `json:"username"`
	First_Name   string `json:"first_name"`
	IsBot        bool   `json:"is_bot"`
	LanguageCode string `json:"language_code"`
}

https://core.telegram.org/bots/api#user

type UserMessageStats

type UserMessageStats struct {
	FloodMsgsLength   []int
	AllMsgsCount      int
	LastMsgTime       int64
	SinceLastMsg      int
	MeanAllMsgsLength int
	Dates             []int64
	Username          string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL