Add LICENSE and first command
This commit may be used as template for making any new Telegram bots. Just check out it, change your variables and start developing new shiny bot! Maybe I need to create bot generator...
This commit is contained in:
@@ -14,32 +14,26 @@ var (
|
||||
c *context.Context
|
||||
log zerolog.Logger
|
||||
|
||||
// Requests is a pointer to initialized Router object
|
||||
Requests *Router
|
||||
// Router is a struct which handles router functions
|
||||
router struct {
|
||||
privateCommands map[string]func(update *telegram.Update)
|
||||
groupCommands map[string]func(update *telegram.Update)
|
||||
privateRegulars map[*regexp.Regexp]func(update *telegram.Update)
|
||||
groupRegulars map[*regexp.Regexp]func(update *telegram.Update)
|
||||
inlineQueries map[*regexp.Regexp]func(update *telegram.Update)
|
||||
}
|
||||
)
|
||||
|
||||
// Router is a struct which handles router functions
|
||||
type Router struct {
|
||||
privateCommands map[string]func(update *telegram.Update)
|
||||
groupCommands map[string]func(update *telegram.Update)
|
||||
privateRegulars map[*regexp.Regexp]func(update *telegram.Update)
|
||||
groupRegulars map[*regexp.Regexp]func(update *telegram.Update)
|
||||
inlineQueries map[*regexp.Regexp]func(update *telegram.Update)
|
||||
}
|
||||
|
||||
// New initializes package
|
||||
func New(cc *context.Context) {
|
||||
c = cc
|
||||
log = c.Logger.With().Str("domain", "router").Int("version", 1).Logger()
|
||||
r := &Router{}
|
||||
|
||||
r.privateCommands = make(map[string]func(update *telegram.Update))
|
||||
r.groupCommands = make(map[string]func(update *telegram.Update))
|
||||
r.privateRegulars = make(map[*regexp.Regexp]func(update *telegram.Update))
|
||||
r.groupRegulars = make(map[*regexp.Regexp]func(update *telegram.Update))
|
||||
r.inlineQueries = make(map[*regexp.Regexp]func(update *telegram.Update))
|
||||
router.privateCommands = make(map[string]func(update *telegram.Update))
|
||||
router.groupCommands = make(map[string]func(update *telegram.Update))
|
||||
router.privateRegulars = make(map[*regexp.Regexp]func(update *telegram.Update))
|
||||
router.groupRegulars = make(map[*regexp.Regexp]func(update *telegram.Update))
|
||||
router.inlineQueries = make(map[*regexp.Regexp]func(update *telegram.Update))
|
||||
|
||||
log.Info().Msg("Initialized requests router")
|
||||
|
||||
Requests = r
|
||||
}
|
||||
|
@@ -19,7 +19,7 @@ var (
|
||||
acceptingForwardsFrom = []int{}
|
||||
)
|
||||
|
||||
func (r *Router) checkForward(update *telegram.Update) error {
|
||||
func checkForward(update *telegram.Update) error {
|
||||
if update.Message.ForwardFrom != nil {
|
||||
log.Debug().Msgf("Processing forward from Telegram ID = %d", update.Message.ForwardFrom.ID)
|
||||
for i := range acceptingForwardsFrom {
|
||||
@@ -32,9 +32,9 @@ func (r *Router) checkForward(update *telegram.Update) error {
|
||||
return errors.New("Can't handle forward from Telegram user with ID =" + strconv.Itoa(update.Message.ForwardFrom.ID))
|
||||
}
|
||||
|
||||
func (r *Router) handleInlineQuery(update *telegram.Update) {
|
||||
func handleInlineQuery(update *telegram.Update) {
|
||||
rxpMatched := false
|
||||
for rxp, function := range r.inlineQueries {
|
||||
for rxp, function := range router.inlineQueries {
|
||||
if rxp.MatchString(update.InlineQuery.Query) {
|
||||
if rxpMatched {
|
||||
log.Warn().Msgf("The message handled more than once: %s, %s", update.InlineQuery.Query, strings.Replace(rxp.String(), "\n", "\\n", -1))
|
||||
@@ -49,7 +49,7 @@ func (r *Router) handleInlineQuery(update *telegram.Update) {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Router) handleRequest(update *telegram.Update, commands map[string]func(*telegram.Update), rxps map[*regexp.Regexp]func(*telegram.Update)) {
|
||||
func handleRequest(update *telegram.Update, commands map[string]func(*telegram.Update), rxps map[*regexp.Regexp]func(*telegram.Update)) {
|
||||
switch {
|
||||
case update.Message.IsCommand():
|
||||
if commands[update.Message.Command()] != nil {
|
||||
@@ -75,61 +75,61 @@ func (r *Router) handleRequest(update *telegram.Update, commands map[string]func
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Router) handleGroupRequest(update *telegram.Update) {
|
||||
r.handleRequest(update, r.groupCommands, r.groupRegulars)
|
||||
func handleGroupRequest(update *telegram.Update) {
|
||||
handleRequest(update, router.groupCommands, router.groupRegulars)
|
||||
}
|
||||
|
||||
func (r *Router) handlePrivateRequest(update *telegram.Update) {
|
||||
r.handleRequest(update, r.privateCommands, r.privateRegulars)
|
||||
func handlePrivateRequest(update *telegram.Update) {
|
||||
handleRequest(update, router.privateCommands, router.privateRegulars)
|
||||
}
|
||||
|
||||
// RegisterPrivateCommand adds function to private commands list
|
||||
func (r *Router) RegisterPrivateCommand(command string, handleFunc func(update *telegram.Update)) {
|
||||
func RegisterPrivateCommand(command string, handleFunc func(update *telegram.Update)) {
|
||||
log.Debug().Msgf("Registering handler for private command /%s", command)
|
||||
r.privateCommands[command] = handleFunc
|
||||
router.privateCommands[command] = handleFunc
|
||||
}
|
||||
|
||||
// RegisterPrivateRegexp adds function to private regexp list
|
||||
func (r *Router) RegisterPrivateRegexp(rxp *regexp.Regexp, handleFunc func(update *telegram.Update)) {
|
||||
func RegisterPrivateRegexp(rxp *regexp.Regexp, handleFunc func(update *telegram.Update)) {
|
||||
log.Debug().Msgf("Registering handler for regular expresson: %s", strings.Replace(rxp.String(), "\n", "\\n", -1))
|
||||
r.privateRegulars[rxp] = handleFunc
|
||||
router.privateRegulars[rxp] = handleFunc
|
||||
}
|
||||
|
||||
// RegisterGroupCommand adds function to group commands list
|
||||
func (r *Router) RegisterGroupCommand(command string, handleFunc func(update *telegram.Update)) {
|
||||
func RegisterGroupCommand(command string, handleFunc func(update *telegram.Update)) {
|
||||
log.Debug().Msgf("Registering handler for group command /%s", command)
|
||||
r.groupCommands[command] = handleFunc
|
||||
router.groupCommands[command] = handleFunc
|
||||
}
|
||||
|
||||
// RegisterGroupRegexp adds function to group regexp list
|
||||
func (r *Router) RegisterGroupRegexp(rxp *regexp.Regexp, handleFunc func(update *telegram.Update)) {
|
||||
func RegisterGroupRegexp(rxp *regexp.Regexp, handleFunc func(update *telegram.Update)) {
|
||||
log.Debug().Msgf("Registering handler for regular expresson: %s", strings.Replace(rxp.String(), "\n", "\\n", -1))
|
||||
r.groupRegulars[rxp] = handleFunc
|
||||
router.groupRegulars[rxp] = handleFunc
|
||||
}
|
||||
|
||||
// RegisterInlineQueryResult adds function to list of inline queries
|
||||
func (r *Router) RegisterInlineQueryResult(rxp *regexp.Regexp, handleFunc func(update *telegram.Update)) {
|
||||
func RegisterInlineQueryResult(rxp *regexp.Regexp, handleFunc func(update *telegram.Update)) {
|
||||
log.Debug().Msgf("Registering handler for inline regular expresson: %s", strings.Replace(rxp.String(), "\n", "\\n", -1))
|
||||
r.inlineQueries[rxp] = handleFunc
|
||||
router.inlineQueries[rxp] = handleFunc
|
||||
}
|
||||
|
||||
// Respond searches for appropriative answer to the request and passes request to found function
|
||||
// If none of the functions can handle this request, it will be warned in log file
|
||||
func (r *Router) Respond(update telegram.Update) {
|
||||
func Respond(update telegram.Update) {
|
||||
switch {
|
||||
case update.Message != nil:
|
||||
if update.Message.Text != "" {
|
||||
if update.Message.ForwardFrom != nil {
|
||||
err := r.checkForward(&update)
|
||||
err := checkForward(&update)
|
||||
if err != nil {
|
||||
log.Warn().Err(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if update.Message.Chat.IsPrivate() {
|
||||
r.handlePrivateRequest(&update)
|
||||
handlePrivateRequest(&update)
|
||||
} else if update.Message.Chat.IsGroup() || update.Message.Chat.IsSuperGroup() {
|
||||
r.handleGroupRequest(&update)
|
||||
handleGroupRequest(&update)
|
||||
} else {
|
||||
log.Debug().Msg("Can't handle update")
|
||||
}
|
||||
@@ -138,7 +138,7 @@ func (r *Router) Respond(update telegram.Update) {
|
||||
}
|
||||
case update.InlineQuery != nil:
|
||||
if update.InlineQuery.Query != "" {
|
||||
r.handleInlineQuery(&update)
|
||||
handleInlineQuery(&update)
|
||||
}
|
||||
default:
|
||||
log.Debug().Msg("Can't handle empty Message for now")
|
||||
|
Reference in New Issue
Block a user