WIP v0.5.0
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
package laniakea
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.nix13.pw/scuroneko/laniakea/tgapi"
|
||||
"git.nix13.pw/scuroneko/laniakea/utils"
|
||||
)
|
||||
|
||||
type MsgContext struct {
|
||||
Bot *Bot
|
||||
Api *Api
|
||||
Api *tgapi.Api
|
||||
|
||||
Msg *Message
|
||||
Update *Update
|
||||
From *User
|
||||
Msg *tgapi.Message
|
||||
Update *tgapi.Update
|
||||
From *tgapi.User
|
||||
CallbackMsgId int
|
||||
CallbackQueryId string
|
||||
FromID int
|
||||
@@ -26,18 +31,18 @@ type AnswerMessage struct {
|
||||
}
|
||||
|
||||
func (ctx *MsgContext) edit(messageId int, text string, keyboard *InlineKeyboard) *AnswerMessage {
|
||||
params := &EditMessageTextP{
|
||||
params := &tgapi.EditMessageTextP{
|
||||
MessageID: messageId,
|
||||
ChatID: ctx.Msg.Chat.ID,
|
||||
Text: text,
|
||||
ParseMode: ParseMD,
|
||||
ParseMode: tgapi.ParseMD,
|
||||
}
|
||||
if keyboard != nil {
|
||||
params.ReplyMarkup = keyboard.Get()
|
||||
}
|
||||
msg, err := ctx.Api.EditMessageText(params)
|
||||
if err != nil {
|
||||
ctx.Api.logger.Errorln(err)
|
||||
ctx.Api.Logger.Errorln(err)
|
||||
return nil
|
||||
}
|
||||
return &AnswerMessage{
|
||||
@@ -49,7 +54,7 @@ func (m *AnswerMessage) Edit(text string) *AnswerMessage {
|
||||
}
|
||||
func (ctx *MsgContext) EditCallback(text string, keyboard *InlineKeyboard) *AnswerMessage {
|
||||
if ctx.CallbackMsgId == 0 {
|
||||
ctx.Api.logger.Errorln("Can't edit non-callback update message")
|
||||
ctx.Api.Logger.Errorln("Can't edit non-callback update message")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -60,18 +65,18 @@ func (ctx *MsgContext) EditCallbackf(format string, keyboard *InlineKeyboard, ar
|
||||
}
|
||||
|
||||
func (ctx *MsgContext) editPhotoText(messageId int, text string, kb *InlineKeyboard) *AnswerMessage {
|
||||
params := &EditMessageCaptionP{
|
||||
params := &tgapi.EditMessageCaptionP{
|
||||
ChatID: ctx.Msg.Chat.ID,
|
||||
MessageID: messageId,
|
||||
Caption: text,
|
||||
ParseMode: ParseMD,
|
||||
ParseMode: tgapi.ParseMD,
|
||||
}
|
||||
if kb != nil {
|
||||
params.ReplyMarkup = kb.Get()
|
||||
}
|
||||
msg, err := ctx.Api.EditMessageCaption(params)
|
||||
if err != nil {
|
||||
ctx.Api.logger.Errorln(err)
|
||||
ctx.Api.Logger.Errorln(err)
|
||||
}
|
||||
return &AnswerMessage{
|
||||
MessageID: msg.MessageID, ctx: ctx, Text: text, IsMedia: true,
|
||||
@@ -79,7 +84,7 @@ func (ctx *MsgContext) editPhotoText(messageId int, text string, kb *InlineKeybo
|
||||
}
|
||||
func (m *AnswerMessage) EditCaption(text string) *AnswerMessage {
|
||||
if m.MessageID == 0 {
|
||||
m.ctx.Api.logger.Errorln("Can't edit caption message, message id is zero")
|
||||
m.ctx.Api.Logger.Errorln("Can't edit caption message, message id is zero")
|
||||
return m
|
||||
}
|
||||
return m.ctx.editPhotoText(m.MessageID, text, nil)
|
||||
@@ -89,10 +94,10 @@ func (m *AnswerMessage) EditCaptionKeyboard(text string, kb *InlineKeyboard) *An
|
||||
}
|
||||
|
||||
func (ctx *MsgContext) answer(text string, keyboard *InlineKeyboard) *AnswerMessage {
|
||||
params := &SendMessageP{
|
||||
params := &tgapi.SendMessageP{
|
||||
ChatID: ctx.Msg.Chat.ID,
|
||||
Text: text,
|
||||
ParseMode: ParseMD,
|
||||
ParseMode: tgapi.ParseMD,
|
||||
}
|
||||
if keyboard != nil {
|
||||
params.ReplyMarkup = keyboard.Get()
|
||||
@@ -100,7 +105,7 @@ func (ctx *MsgContext) answer(text string, keyboard *InlineKeyboard) *AnswerMess
|
||||
|
||||
msg, err := ctx.Api.SendMessage(params)
|
||||
if err != nil {
|
||||
ctx.Api.logger.Errorln(err)
|
||||
ctx.Api.Logger.Errorln(err)
|
||||
return nil
|
||||
}
|
||||
return &AnswerMessage{
|
||||
@@ -118,10 +123,10 @@ func (ctx *MsgContext) Keyboard(text string, kb *InlineKeyboard) *AnswerMessage
|
||||
}
|
||||
|
||||
func (ctx *MsgContext) answerPhoto(photoId, text string, kb *InlineKeyboard) *AnswerMessage {
|
||||
params := &SendPhotoP{
|
||||
params := &tgapi.SendPhotoP{
|
||||
ChatID: ctx.Msg.Chat.ID,
|
||||
Caption: text,
|
||||
ParseMode: ParseMD,
|
||||
ParseMode: tgapi.ParseMD,
|
||||
Photo: photoId,
|
||||
}
|
||||
if kb != nil {
|
||||
@@ -129,7 +134,7 @@ func (ctx *MsgContext) answerPhoto(photoId, text string, kb *InlineKeyboard) *An
|
||||
}
|
||||
msg, err := ctx.Api.SendPhoto(params)
|
||||
if err != nil {
|
||||
ctx.Api.logger.Errorln(err)
|
||||
ctx.Api.Logger.Errorln(err)
|
||||
return &AnswerMessage{
|
||||
ctx: ctx, Text: text, IsMedia: true,
|
||||
}
|
||||
@@ -146,12 +151,12 @@ func (ctx *MsgContext) AnswerPhotoKeyboard(photoId, text string, kb *InlineKeybo
|
||||
}
|
||||
|
||||
func (ctx *MsgContext) delete(messageId int) {
|
||||
_, err := ctx.Api.DeleteMessage(&DeleteMessageP{
|
||||
_, err := ctx.Api.DeleteMessage(&tgapi.DeleteMessageP{
|
||||
ChatID: ctx.Msg.Chat.ID,
|
||||
MessageID: messageId,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Api.logger.Errorln(err)
|
||||
ctx.Api.Logger.Errorln(err)
|
||||
}
|
||||
}
|
||||
func (m *AnswerMessage) Delete() {
|
||||
@@ -165,12 +170,12 @@ func (ctx *MsgContext) answerCallbackQuery(url, text string, showAlert bool) {
|
||||
if len(ctx.CallbackQueryId) == 0 {
|
||||
return
|
||||
}
|
||||
_, err := ctx.Api.AnswerCallbackQuery(&AnswerCallbackQueryP{
|
||||
_, err := ctx.Api.AnswerCallbackQuery(&tgapi.AnswerCallbackQueryP{
|
||||
CallbackQueryID: ctx.CallbackQueryId,
|
||||
Text: text, ShowAlert: showAlert, URL: url,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Api.logger.Errorln(err)
|
||||
ctx.Api.Logger.Errorln(err)
|
||||
}
|
||||
}
|
||||
func (ctx *MsgContext) AnswerCbQuery() {
|
||||
@@ -186,24 +191,24 @@ func (ctx *MsgContext) AnswerCbQueryUrl(u string) {
|
||||
ctx.answerCallbackQuery(u, "", false)
|
||||
}
|
||||
|
||||
func (ctx *MsgContext) SendAction(action ChatActions) {
|
||||
_, err := ctx.Api.SendChatAction(SendChatActionP{
|
||||
func (ctx *MsgContext) SendAction(action tgapi.ChatActionType) {
|
||||
_, err := ctx.Api.SendChatAction(tgapi.SendChatActionP{
|
||||
ChatID: ctx.Msg.Chat.ID, Action: action,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Api.logger.Errorln(err)
|
||||
ctx.Api.Logger.Errorln(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (ctx *MsgContext) error(err error) {
|
||||
text := fmt.Sprintf(ctx.Bot.errorTemplate, EscapeMarkdown(err.Error()))
|
||||
text := fmt.Sprintf(ctx.Bot.errorTemplate, utils.EscapeMarkdown(err.Error()))
|
||||
|
||||
if ctx.CallbackQueryId != "" {
|
||||
ctx.answerCallbackQuery("", text, false)
|
||||
} else {
|
||||
ctx.answer(text, nil)
|
||||
}
|
||||
ctx.Bot.logger.Errorln(err)
|
||||
ctx.Bot.Logger().Errorln(err)
|
||||
}
|
||||
func (ctx *MsgContext) Error(err error) {
|
||||
ctx.error(err)
|
||||
|
||||
Reference in New Issue
Block a user