package nechotron import ( "context" "fmt" "log" "github.com/NicoNex/echotron/v3" "github.com/google/uuid" ) type U echotron.Update var emptyOpts = echotron.MessageOptions{} type Event interface { Upd() *echotron.Update } type Update struct { U Bot *bot UpdateID uuid.UUID Ctx context.Context } type UpdateCommand struct { Update Param string } func (u *Update) Upd() *echotron.Update { return (*echotron.Update)(&u.U) } func (u *Update) Chat() echotron.Chat { if u.IsMessage() { return u.Message.Chat } if u.IsCallback() { return u.CallbackQuery.Message.Chat } if u.EditedMessage != nil { return u.EditedMessage.Chat } if u.ChatMember != nil { return u.ChatMember.Chat } if u.MyChatMember != nil { return u.MyChatMember.Chat } log.Fatalf("[%s] Can't get ChatID of update %v+", u.UpdateID, u.U) return echotron.Chat{} } func (u *Update) ChatID() int64 { if u.IsMessage() { return u.Message.Chat.ID } if u.IsCallback() { return u.CallbackQuery.Message.Chat.ID } log.Fatalf("[%s] Can't get ChatID of update %v+", u.UpdateID, u.U) return 0 } func (u *Update) MessageID() int { if u.IsMessage() { return u.Message.ID } if u.IsCallback() { return u.CallbackQuery.Message.ID } log.Fatalf("[%s] Can't get ChatID of update %v+", u.UpdateID, u.U) return 0 } func (u *Update) AnswerText(text string, options *echotron.MessageOptions) (echotron.APIResponseMessage, error) { return u.Bot.SendMessage(text, u.ChatID(), options) } func (u *Update) EditText(text string, options *echotron.MessageTextOptions) (echotron.APIResponse, error) { return u.Bot.EditMessageText(text, echotron.NewMessageID(u.ChatID(), u.MessageID()), options) } func (u *Update) AnswerPlain(text string) (echotron.APIResponseMessage, error) { return u.Bot.SendMessage(text, u.ChatID(), &emptyOpts) } func (u *Update) AnswerMarkdown(text string) (echotron.APIResponseMessage, error) { return u.Bot.SendMessage(text, u.ChatID(), &echotron.MessageOptions{ ParseMode: echotron.MarkdownV2, }) } func (u *Update) DeleteMessage() (echotron.APIResponse, error) { return u.Bot.DeleteMessage(u.ChatID(), u.MessageID()) } type MessageType uint8 const ( MESSAGE_UNKNOWN MessageType = iota MESSAGE_TEXT MESSAGE_PHOTO MESSAGE_VIDEO MESSAGE_DOCUMENT MESSAGE_AUDIO MESSAGE_VOICE MESSAGE_VIDEONOTE MESSAGE_CALLBACK ) func (u *Update) IsMessage() bool { return u.Message != nil } func (u *Update) IsCallback() bool { return u.CallbackQuery != nil } func (u *Update) IsButton(b *Button) bool { if u.IsText() && u.Text() == b.text { return true } if u.IsCallback() && u.CallbackQuery.Data == b.query { return true } return false } func (u *Update) IsText() bool { return u.IsMessage() && u.Message.Text != "" } func (u *Update) IsPhoto() bool { return u.IsMessage() && u.Message.Photo != nil } func (u *Update) IsVideo() bool { return u.IsMessage() && u.Message.Video != nil } func (u *Update) IsDocument() bool { return u.IsMessage() && u.Message.Document != nil } func (u *Update) IsAudio() bool { return u.IsMessage() && u.Message.Audio != nil } func (u *Update) IsVoice() bool { return u.IsMessage() && u.Message.Voice != nil } func (u *Update) IsVideoNote() bool { return u.IsMessage() && u.Message.VideoNote != nil } func (u *Update) Type() MessageType { if u.IsCallback() { return MESSAGE_CALLBACK } if u.IsText() { return MESSAGE_TEXT } if u.IsAudio() { return MESSAGE_AUDIO } if u.IsVideo() { return MESSAGE_VIDEO } if u.IsPhoto() { return MESSAGE_PHOTO } if u.IsDocument() { return MESSAGE_DOCUMENT } if u.IsVoice() { return MESSAGE_VOICE } if u.IsVideoNote() { return MESSAGE_VIDEONOTE } return MESSAGE_UNKNOWN } func (u *Update) From() *echotron.User { if u.IsMessage() { return u.Message.From } if u.EditedMessage != nil { return u.EditedMessage.From } if u.ChatMember != nil { return &u.ChatMember.From } if u.MyChatMember != nil { return &u.MyChatMember.From } return u.CallbackQuery.From } func (u *Update) Text() string { if u.IsText() { return u.Message.Text } if u.IsMessage() { return u.Message.Caption } return "" } func (u *Update) Entities() []*echotron.MessageEntity { if u.IsText() { return u.Message.Entities } return u.Message.CaptionEntities } func IsUserAdmin(u *Update) bool { if IsPrivate(u) { return true } member, err := u.Bot.GetChatMember(u.ChatID(), u.From().ID) if err != nil { return false } return member.Result.Status == "administrator" || member.Result.Status == "creator" } func IsPrivate(u *Update) bool { return u.ChatID() > 0 } func IsForum(u *Update) bool { return u.Chat().IsForum } func (u *Update) LogError(text string, e error, send bool) { if text != "" { text = "Ошибка: " + text } else { text = "Произошла внутренняя ошибка" } log.Printf("ERROR :: [%s] :: %s: %v", u.UpdateID.String(), text, e) if send { u.AnswerMarkdown(fmt.Sprintf("%s\nКод запроса: `%s`", text, u.UpdateID.String())) } }