Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
83dca1ab39
|
|||
|
2cc2f96f02
|
|||
|
9d18bef97e
|
32
api.go
32
api.go
@@ -19,13 +19,10 @@ type Api struct {
|
||||
func NewAPI(token string) *Api {
|
||||
l := slog.CreateLogger().Level(GetLoggerLevel()).Prefix("API")
|
||||
l.AddWriter(l.CreateJsonStdoutWriter())
|
||||
return &Api{
|
||||
token: token,
|
||||
logger: l,
|
||||
return &Api{token, l}
|
||||
}
|
||||
}
|
||||
func (api *Api) CloseApi() {
|
||||
api.logger.Close()
|
||||
func (api *Api) CloseApi() error {
|
||||
return api.logger.Close()
|
||||
}
|
||||
|
||||
type ApiResponse[R any] struct {
|
||||
@@ -44,25 +41,25 @@ func NewRequest[R, P any](method string, params P) TelegramRequest[R, P] {
|
||||
return TelegramRequest[R, P]{method: method, params: params}
|
||||
}
|
||||
func (r TelegramRequest[R, P]) Do(api *Api) (*R, error) {
|
||||
var buf bytes.Buffer
|
||||
err := json.NewEncoder(&buf).Encode(r.params)
|
||||
buf := bytes.NewBuffer(nil)
|
||||
err := json.NewEncoder(buf).Encode(r.params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u := fmt.Sprintf("https://api.telegram.org/bot%s/%s", api.token, r.method)
|
||||
if api.logger != nil {
|
||||
api.logger.Debugln(strings.ReplaceAll(fmt.Sprintf(
|
||||
"POST https://api.telegram.org/bot%s/%s %s",
|
||||
"<TOKEN>", r.method, buf.String(),
|
||||
), "\n", ""))
|
||||
"POST %s %s", u, buf.String(),
|
||||
), api.token, "<TOKEN>"))
|
||||
}
|
||||
|
||||
req, err := http.Post(fmt.Sprintf("https://api.telegram.org/bot%s/%s", api.token, r.method), "application/json", &buf)
|
||||
res, err := http.Post(u, "application/json", buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer req.Body.Close()
|
||||
data, err := io.ReadAll(req.Body)
|
||||
defer res.Body.Close()
|
||||
data, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -84,13 +81,8 @@ func (r TelegramRequest[R, P]) Do(api *Api) (*R, error) {
|
||||
}
|
||||
|
||||
func (b *Bot) GetFileByLink(link string) ([]byte, error) {
|
||||
c := http.DefaultClient
|
||||
u := fmt.Sprintf("https://api.telegram.org/file/bot%s/%s", b.token, link)
|
||||
req, err := http.NewRequest("GET", u, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, err := c.Do(req)
|
||||
res, err := http.Get(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
6
go.mod
6
go.mod
@@ -1,9 +1,9 @@
|
||||
module git.nix13.pw/scuroneko/laniakea
|
||||
|
||||
go 1.25.6
|
||||
go 1.25
|
||||
|
||||
require (
|
||||
git.nix13.pw/scuroneko/extypes v1.0.2
|
||||
git.nix13.pw/scuroneko/extypes v1.1.0
|
||||
git.nix13.pw/scuroneko/slog v1.0.2
|
||||
github.com/redis/go-redis/v9 v9.17.3
|
||||
github.com/vinovest/sqlx v1.7.1
|
||||
@@ -14,7 +14,7 @@ require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/fatih/color v1.18.0 // indirect
|
||||
github.com/klauspost/compress v1.18.3 // indirect
|
||||
github.com/klauspost/compress v1.18.4 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/muir/sqltoken v0.2.1 // indirect
|
||||
|
||||
8
go.sum
8
go.sum
@@ -1,7 +1,7 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
git.nix13.pw/scuroneko/extypes v1.0.2 h1:Qz1InLccaB9crXY33oGrSetPHePKfQAUqW/p/iYXmJk=
|
||||
git.nix13.pw/scuroneko/extypes v1.0.2/go.mod h1:uZVs8Yo3RrYAG9dMad6qR6lsYY67t+459D9c65QAYAw=
|
||||
git.nix13.pw/scuroneko/extypes v1.1.0 h1:kdAraybAqQgVhArVkVfrIi7KVEX8HgTr8mzbIZAAAqg=
|
||||
git.nix13.pw/scuroneko/extypes v1.1.0/go.mod h1:uZVs8Yo3RrYAG9dMad6qR6lsYY67t+459D9c65QAYAw=
|
||||
git.nix13.pw/scuroneko/slog v1.0.2 h1:vZyUROygxC2d5FJHUQM/30xFEHY1JT/aweDZXA4rm2g=
|
||||
git.nix13.pw/scuroneko/slog v1.0.2/go.mod h1:3Qm2wzkR5KjwOponMfG7TcGSDjmYaFqRAmLvSPTuWJI=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
@@ -20,8 +20,8 @@ github.com/go-sql-driver/mysql v1.9.0 h1:Y0zIbQXhQKmQgTp44Y1dp3wTXcn804QoTptLZT1
|
||||
github.com/go-sql-driver/mysql v1.9.0/go.mod h1:pDetrLJeA3oMujJuvXc8RJoasr589B6A9fwzD3QMrqw=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/klauspost/compress v1.18.3 h1:9PJRvfbmTabkOX8moIpXPbMMbYN60bWImDDU7L+/6zw=
|
||||
github.com/klauspost/compress v1.18.3/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
|
||||
github.com/klauspost/compress v1.18.4 h1:RPhnKRAQ4Fh8zU2FY/6ZFDwTVTxgJ/EMydqSTzE9a2c=
|
||||
github.com/klauspost/compress v1.18.4/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||
|
||||
74
keyboard.go
74
keyboard.go
@@ -3,51 +3,103 @@ package laniakea
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.nix13.pw/scuroneko/extypes"
|
||||
)
|
||||
|
||||
const (
|
||||
ButtonStyleDanger KeyboardButtonStyle = "danger"
|
||||
ButtonStyleSuccess KeyboardButtonStyle = "success"
|
||||
ButtonStylePrimary KeyboardButtonStyle = "primary"
|
||||
)
|
||||
|
||||
type InlineKbButtonBuilder struct {
|
||||
text string
|
||||
iconCustomEmojiID string
|
||||
style KeyboardButtonStyle
|
||||
url string
|
||||
callbackData string
|
||||
}
|
||||
|
||||
func NewInlineKbButton(text string) InlineKbButtonBuilder {
|
||||
return InlineKbButtonBuilder{text: text}
|
||||
}
|
||||
func (b InlineKbButtonBuilder) SetIconCustomEmojiId(id string) InlineKbButtonBuilder {
|
||||
b.iconCustomEmojiID = id
|
||||
return b
|
||||
}
|
||||
func (b InlineKbButtonBuilder) SetStyle(style KeyboardButtonStyle) InlineKbButtonBuilder {
|
||||
b.style = style
|
||||
return b
|
||||
}
|
||||
func (b InlineKbButtonBuilder) SetUrl(url string) InlineKbButtonBuilder {
|
||||
b.url = url
|
||||
return b
|
||||
}
|
||||
func (b InlineKbButtonBuilder) SetCallbackData(cmd string, args ...any) InlineKbButtonBuilder {
|
||||
b.callbackData = NewCallbackData(cmd, args...).ToJson()
|
||||
return b
|
||||
}
|
||||
|
||||
func (b InlineKbButtonBuilder) build() InlineKeyboardButton {
|
||||
return InlineKeyboardButton{
|
||||
Text: b.text,
|
||||
URL: b.url,
|
||||
Style: b.style,
|
||||
IconCustomEmojiID: b.iconCustomEmojiID,
|
||||
CallbackData: b.callbackData,
|
||||
}
|
||||
}
|
||||
|
||||
type InlineKeyboard struct {
|
||||
CurrentLine []InlineKeyboardButton
|
||||
CurrentLine extypes.Slice[InlineKeyboardButton]
|
||||
Lines [][]InlineKeyboardButton
|
||||
maxRow int
|
||||
}
|
||||
|
||||
func NewInlineKeyboard(maxRow int) *InlineKeyboard {
|
||||
return &InlineKeyboard{
|
||||
CurrentLine: make([]InlineKeyboardButton, 0),
|
||||
CurrentLine: make(extypes.Slice[InlineKeyboardButton], 0),
|
||||
Lines: make([][]InlineKeyboardButton, 0),
|
||||
maxRow: maxRow,
|
||||
}
|
||||
}
|
||||
|
||||
func (in *InlineKeyboard) append(button InlineKeyboardButton) *InlineKeyboard {
|
||||
if len(in.CurrentLine) == in.maxRow {
|
||||
if in.CurrentLine.Len() == in.maxRow {
|
||||
in.AddLine()
|
||||
}
|
||||
in.CurrentLine = append(in.CurrentLine, button)
|
||||
in.CurrentLine = in.CurrentLine.Push(button)
|
||||
return in
|
||||
}
|
||||
func (in *InlineKeyboard) AddUrlButton(text, url string) *InlineKeyboard {
|
||||
return in.append(InlineKeyboardButton{Text: text, URL: url})
|
||||
}
|
||||
func (in *InlineKeyboard) AddCallbackButton(text string, cmd string, args ...any) *InlineKeyboard {
|
||||
return in.append(InlineKeyboardButton{Text: text, CallbackData: NewCallbackData(cmd, args...).ToJson()})
|
||||
return in.append(InlineKeyboardButton{
|
||||
Text: text, CallbackData: NewCallbackData(cmd, args...).ToJson(),
|
||||
})
|
||||
}
|
||||
func (in *InlineKeyboard) AddButton(b InlineKbButtonBuilder) *InlineKeyboard {
|
||||
return in.append(b.build())
|
||||
}
|
||||
func (in *InlineKeyboard) AddCustomButton(button InlineKeyboardButton) *InlineKeyboard {
|
||||
return in.append(button)
|
||||
}
|
||||
|
||||
func (in *InlineKeyboard) AddLine() *InlineKeyboard {
|
||||
if len(in.CurrentLine) == 0 {
|
||||
if in.CurrentLine.Len() == 0 {
|
||||
return in
|
||||
}
|
||||
in.Lines = append(in.Lines, in.CurrentLine)
|
||||
in.CurrentLine = make([]InlineKeyboardButton, 0)
|
||||
in.CurrentLine = make(extypes.Slice[InlineKeyboardButton], 0)
|
||||
return in
|
||||
}
|
||||
func (in *InlineKeyboard) Get() *InlineKeyboardMarkup {
|
||||
if len(in.CurrentLine) > 0 {
|
||||
if in.CurrentLine.Len() > 0 {
|
||||
in.Lines = append(in.Lines, in.CurrentLine)
|
||||
}
|
||||
return &InlineKeyboardMarkup{
|
||||
InlineKeyboard: in.Lines,
|
||||
}
|
||||
return &InlineKeyboardMarkup{InlineKeyboard: in.Lines}
|
||||
}
|
||||
|
||||
type CallbackData struct {
|
||||
|
||||
14
methods.go
14
methods.go
@@ -2,7 +2,6 @@ package laniakea
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type EmptyParams struct{}
|
||||
@@ -25,26 +24,25 @@ func (b *Bot) Updates() ([]*Update, error) {
|
||||
req := NewRequest[[]*Update]("getUpdates", params)
|
||||
res, err := req.Do(b.api)
|
||||
if err != nil {
|
||||
return []*Update{}, err
|
||||
return nil, err
|
||||
}
|
||||
updates := *res
|
||||
|
||||
for _, u := range updates {
|
||||
for _, u := range *res {
|
||||
b.updateOffset = u.UpdateID + 1
|
||||
err = b.updateQueue.Enqueue(u)
|
||||
if err != nil {
|
||||
return updates, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if b.debug && b.requestLogger != nil {
|
||||
if b.requestLogger != nil {
|
||||
j, err := json.Marshal(u)
|
||||
if err != nil {
|
||||
b.logger.Error(err)
|
||||
}
|
||||
b.requestLogger.Debugln(fmt.Sprintf("UPDATE %s", j))
|
||||
b.requestLogger.Debugf("UPDATE %s\n", j)
|
||||
}
|
||||
}
|
||||
return updates, err
|
||||
return *res, err
|
||||
}
|
||||
|
||||
func (api *Api) GetMe() (*User, error) {
|
||||
|
||||
@@ -78,7 +78,6 @@ func Encode[T any](w *multipart.Writer, req T) error {
|
||||
if err == nil {
|
||||
_, err = fw.Write([]byte(strconv.FormatBool(field.Bool())))
|
||||
}
|
||||
|
||||
case reflect.Slice:
|
||||
if field.Type().Elem().Kind() == reflect.Uint8 && !field.IsNil() {
|
||||
filename := fieldType.Tag.Get("filename")
|
||||
|
||||
3
types.go
3
types.go
@@ -128,10 +128,13 @@ type InlineKeyboardMarkup struct {
|
||||
InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard,omitempty"`
|
||||
}
|
||||
|
||||
type KeyboardButtonStyle string
|
||||
type InlineKeyboardButton struct {
|
||||
Text string `json:"text"`
|
||||
URL string `json:"url,omitempty"`
|
||||
CallbackData string `json:"callback_data,omitempty"`
|
||||
Style KeyboardButtonStyle `json:"style,omitempty"`
|
||||
IconCustomEmojiID string `json:"icon_custom_emoji_id,omitempty"`
|
||||
}
|
||||
|
||||
type ReplyKeyboardMarkup struct {
|
||||
|
||||
@@ -22,8 +22,8 @@ func NewUploader(api *Api) *Uploader {
|
||||
logger.AddWriter(logger.CreateJsonStdoutWriter())
|
||||
return &Uploader{api, logger}
|
||||
}
|
||||
func (u *Uploader) Close() {
|
||||
u.logger.Close()
|
||||
func (u *Uploader) Close() error {
|
||||
return u.logger.Close()
|
||||
}
|
||||
|
||||
type UploaderFileType string
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package laniakea
|
||||
|
||||
const (
|
||||
VersionString = "0.4.0"
|
||||
VersionString = "0.4.2"
|
||||
VersionMajor = 0
|
||||
VersionMinor = 4
|
||||
VersionPatch = 0
|
||||
VersionPatch = 2
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user