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{}
|
|
|
|
|
|
2023-01-22 23:54:54 +03:00
|
|
|
|
type Event interface {
|
2023-01-24 00:37:08 +03:00
|
|
|
|
Upd() *echotron.Update
|
2023-01-22 23:54:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-19 23:08:59 +03:00
|
|
|
|
type Update struct {
|
|
|
|
|
U
|
|
|
|
|
Bot *bot
|
|
|
|
|
UpdateID uuid.UUID
|
|
|
|
|
Ctx context.Context
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-22 23:54:54 +03:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-24 02:25:08 +03:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2023-01-24 02:25:08 +03:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-24 02:25:08 +03:00
|
|
|
|
func IsUserAdmin(u *Update) bool {
|
|
|
|
|
if IsPrivate(u) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2023-01-22 23:54:54 +03:00
|
|
|
|
member, err := u.Bot.GetChatMember(u.ChatID(), u.From().ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
2023-01-19 23:08:59 +03:00
|
|
|
|
}
|
2023-01-22 23:54:54 +03:00
|
|
|
|
return member.Result.Status == "administrator" || member.Result.Status == "creator"
|
2023-01-19 23:08:59 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-24 02:25:08 +03:00
|
|
|
|
func IsPrivate(u *Update) bool {
|
|
|
|
|
return u.ChatID() > 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func IsForum(u *Update) bool {
|
|
|
|
|
return u.Chat().IsForum
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-19 23:08:59 +03:00
|
|
|
|
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()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|