laniakea/methods.go

59 lines
1.2 KiB
Go
Raw Normal View History

2024-12-16 00:52:43 +03:00
package laniakea
import "fmt"
var NO_PARAMS = make(map[string]interface{})
func (b *Bot) GetUpdates() ([]*Update, error) {
params := make(map[string]interface{})
params["offset"] = b.updateOffset
params["timeout"] = 30
data, err := b.request("getUpdates", params)
if err != nil {
return nil, err
}
res := make([]*Update, 0)
for _, u := range data["data"].([]interface{}) {
updateObj := new(Update)
err = MapToStruct(u.(map[string]interface{}), updateObj)
if err != nil {
return res, err
}
b.updateOffset = updateObj.UpdateID + 1
err = b.updateQueue.Enqueue(updateObj)
if err != nil {
return res, err
}
res = append(res, updateObj)
if b.debug {
j, err := MapToJson(u.(map[string]interface{}))
if err != nil {
b.logger.Error(err)
}
b.logger.Debug(fmt.Sprintf("New update: %s\n", j))
}
}
return res, err
}
type SendMessageP struct {
ChatID int `json:"chat_id"`
Text string `json:"text"`
}
func (b *Bot) SendMessage(params *SendMessageP) (*Message, error) {
dataP, err := StructToMap(params)
if err != nil {
return nil, err
}
data, err := b.request("sendMessage", dataP)
if err != nil {
return nil, err
}
message := new(Message)
err = MapToStruct(data, message)
return message, err
}