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