nechotron/update.go

259 lines
5.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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) 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
}
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(), &echotron.MessageOptions{
MessageThreadID: u.ThreadID(),
})
}
func (u *Update) AnswerMarkdown(text string) (echotron.APIResponseMessage, error) {
return u.Bot.SendMessage(text, u.ChatID(), &echotron.MessageOptions{
ParseMode: echotron.MarkdownV2,
MessageThreadID: u.ThreadID(),
})
}
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) IsReply() bool {
return u.IsMessage() && u.Message.ReplyToMessage != 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 GetText(u *echotron.Message) string {
if u.Text != "" {
return u.Text
}
if u.Caption != "" {
return u.Caption
}
return ""
}
func (u *Update) Text() string {
if u.IsText() {
return u.Message.Text
}
if u.IsMessage() {
return u.Message.Caption
}
return ""
}
func (u *Update) Callback() string {
if u.IsCallback() {
return u.CallbackQuery.Data
}
return ""
}
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 = "Произошла внутренняя ошибка"
}
log.Printf("ERROR :: [%s] :: %s: %v", u.UpdateID.String(), text, e)
if send {
u.AnswerMarkdown(fmt.Sprintf("%s\nКод запроса: `%s`", text, u.UpdateID.String()))
}
}