nechotron/update.go

252 lines
5.1 KiB
Go
Raw Normal View History

2023-01-19 23:08:59 +03:00
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 {
2023-01-24 00:37:08 +03:00
Upd() *echotron.Update
}
2023-01-19 23:08:59 +03:00
type Update struct {
U
Bot *bot
UpdateID uuid.UUID
Ctx context.Context
}
type UpdateCommand struct {
Update
Param string
}
2023-01-19 23:08:59 +03:00
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{}
}
2023-01-19 23:08:59 +03:00
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
}
2023-01-24 23:16:58 +03:00
func (u *Update) ThreadID() int64 {
if !u.Chat().IsForum {
return 0
}
if u.IsMessage() {
return int64(u.Message.ThreadID)
}
if u.IsCallback() {
return int64(u.CallbackQuery.Message.ThreadID)
}
return 0
}
2023-01-19 23:08:59 +03:00
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) {
2023-01-24 23:16:58 +03:00
return u.Bot.SendMessage(text, u.ChatID(), &echotron.MessageOptions{
MessageThreadID: u.ThreadID(),
})
2023-01-19 23:08:59 +03:00
}
func (u *Update) AnswerMarkdown(text string) (echotron.APIResponseMessage, error) {
return u.Bot.SendMessage(text, u.ChatID(), &echotron.MessageOptions{
2023-01-24 23:16:58 +03:00
ParseMode: echotron.MarkdownV2,
MessageThreadID: u.ThreadID(),
2023-01-19 23:08:59 +03:00
})
}
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
}
2023-01-24 23:16:58 +03:00
func (u *Update) IsReply() bool {
return u.IsMessage() && u.Message.ReplyToMessage != nil
}
2023-01-19 23:08:59 +03:00
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
}
2023-01-19 23:08:59 +03:00
return u.CallbackQuery.From
}
2023-01-24 23:16:58 +03:00
func GetText(u *echotron.Message) string {
if u.Text != "" {
return u.Text
}
if u.Caption != "" {
return u.Caption
}
return ""
}
2023-01-19 23:08:59 +03:00
func (u *Update) Text() string {
if u.IsText() {
return u.Message.Text
}
2023-01-24 03:08:34 +03:00
if u.IsMessage() {
return u.Message.Caption
}
return ""
2023-01-19 23:08:59 +03:00
}
func (u *Update) Entities() []*echotron.MessageEntity {
if u.IsText() {
return u.Message.Entities
}
return u.Message.CaptionEntities
}
func (u *Update) LogError(text string, e error, send bool) {
if text != "" {
text = "Ошибка: " + text
} else {
text = "Произошла внутренняя ошибка"
}
2023-01-20 16:32:13 +03:00
log.Printf("ERROR :: [%s] :: %s: %v", u.UpdateID.String(), text, e)
2023-01-19 23:08:59 +03:00
if send {
u.AnswerMarkdown(fmt.Sprintf("%s\nКод запроса: `%s`", text, u.UpdateID.String()))
}
}