197 lines
4.0 KiB
Go
197 lines
4.0 KiB
Go
package nechotron
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"log"
|
||
|
||
"github.com/NicoNex/echotron/v3"
|
||
"github.com/google/uuid"
|
||
)
|
||
|
||
type U echotron.Update
|
||
|
||
var emptyOpts = echotron.MessageOptions{}
|
||
|
||
type Update struct {
|
||
U
|
||
Bot *bot
|
||
UpdateID uuid.UUID
|
||
Ctx context.Context
|
||
}
|
||
|
||
func (u *Update) Upd() *echotron.Update {
|
||
return (*echotron.Update)(&u.U)
|
||
}
|
||
|
||
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
|
||
}
|
||
return u.CallbackQuery.From
|
||
}
|
||
|
||
func (u *Update) Text() string {
|
||
if u.IsText() {
|
||
return u.Message.Text
|
||
}
|
||
return u.Message.Caption
|
||
}
|
||
|
||
func (u *Update) Entities() []*echotron.MessageEntity {
|
||
if u.IsText() {
|
||
return u.Message.Entities
|
||
}
|
||
return u.Message.CaptionEntities
|
||
}
|
||
|
||
func (u *Update) IsUserAdmin() bool {
|
||
ids := []int64{
|
||
60441930, // v.rud
|
||
327487258, // vika shet
|
||
}
|
||
from := u.From()
|
||
for i := range ids {
|
||
if from.ID == ids[i] {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
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()))
|
||
}
|
||
|
||
}
|