mirror of
https://github.com/retailcrm/mg-bot-api-client-go.git
synced 2024-11-21 20:36:05 +03:00
commit
c7b0043002
@ -3,17 +3,28 @@ package v1
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
letterIdxBits = 6 // 6 bits to represent a letter index
|
||||||
|
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
|
||||||
|
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
|
||||||
|
)
|
||||||
|
|
||||||
|
var src = rand.NewSource(time.Now().UnixNano())
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
if os.Getenv("DEVELOPER_NODE") == "1" {
|
if os.Getenv("DEVELOPER_NODE") == "1" {
|
||||||
err := godotenv.Load("../.env")
|
err := godotenv.Load("../.env")
|
||||||
@ -129,7 +140,6 @@ func TestMgClient_Members(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotEmpty(t, data)
|
|
||||||
|
|
||||||
for _, member := range data {
|
for _, member := range data {
|
||||||
assert.NotEmpty(t, member.ChatID)
|
assert.NotEmpty(t, member.ChatID)
|
||||||
@ -209,6 +219,7 @@ func TestMgClient_MessageSendText(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NotEmpty(t, data.MessageID)
|
assert.NotEmpty(t, data.MessageID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMgClient_MessageSendProduct(t *testing.T) {
|
func TestMgClient_MessageSendProduct(t *testing.T) {
|
||||||
c := client()
|
c := client()
|
||||||
|
|
||||||
@ -240,6 +251,60 @@ func TestMgClient_MessageSendProduct(t *testing.T) {
|
|||||||
t.Logf("%v", msg)
|
t.Logf("%v", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMgClient_MessageSendOrder(t *testing.T) {
|
||||||
|
c := client()
|
||||||
|
|
||||||
|
msg, _, err := c.MessageSend(MessageSendRequest{
|
||||||
|
Type: MsgTypeOrder,
|
||||||
|
ChatID: 5,
|
||||||
|
Scope: "public",
|
||||||
|
Order: &MessageOrder{
|
||||||
|
Number: RandStringBytesMaskImprSrc(7),
|
||||||
|
Cost: &MessageOrderCost{
|
||||||
|
Value: 29900,
|
||||||
|
Currency: MsgCurrencyRub,
|
||||||
|
},
|
||||||
|
Status: &MessageOrderStatus{
|
||||||
|
Code: MsgOrderStatusCodeNew,
|
||||||
|
Name: "Новый",
|
||||||
|
},
|
||||||
|
Delivery: &MessageOrderDelivery{
|
||||||
|
Name: "Курьерская доставка",
|
||||||
|
Address: "г. Москва, Проспект Мира, 9",
|
||||||
|
Amount: &MessageOrderCost{
|
||||||
|
Value: 1100,
|
||||||
|
Currency: MsgCurrencyRub,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Items: []MessageOrderItem{
|
||||||
|
{
|
||||||
|
Name: "iPhone 6",
|
||||||
|
Price: &MessageOrderCost{
|
||||||
|
Value: 29900,
|
||||||
|
Currency: MsgCurrencyRub,
|
||||||
|
},
|
||||||
|
Quantity: &MessageOrderQuantity{
|
||||||
|
Value: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
t.Logf("%v", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMgClient_RandomStringGenerator(t *testing.T) {
|
||||||
|
rnd := RandStringBytesMaskImprSrc(7)
|
||||||
|
assert.NotEmpty(t, rnd)
|
||||||
|
t.Logf("%v", rnd)
|
||||||
|
}
|
||||||
|
|
||||||
func TestMgClient_MessageEdit(t *testing.T) {
|
func TestMgClient_MessageEdit(t *testing.T) {
|
||||||
c := client()
|
c := client()
|
||||||
i, err := strconv.ParseUint(os.Getenv("MG_BOT_CHAT"), 10, 64)
|
i, err := strconv.ParseUint(os.Getenv("MG_BOT_CHAT"), 10, 64)
|
||||||
@ -365,3 +430,21 @@ func TestMgClient_WsMeta(t *testing.T) {
|
|||||||
assert.Equal(t, resUrl, url)
|
assert.Equal(t, resUrl, url)
|
||||||
assert.Equal(t, resToken, headers["X-Bot-Token"][0])
|
assert.Equal(t, resToken, headers["X-Bot-Token"][0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RandStringBytesMaskImprSrc(n int) string {
|
||||||
|
b := make([]byte, n)
|
||||||
|
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
|
||||||
|
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
|
||||||
|
if remain == 0 {
|
||||||
|
cache, remain = src.Int63(), letterIdxMax
|
||||||
|
}
|
||||||
|
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
|
||||||
|
b[i] = letterBytes[idx]
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
cache >>= letterIdxBits
|
||||||
|
remain--
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
45
v1/types.go
45
v1/types.go
@ -48,6 +48,20 @@ const (
|
|||||||
MsgTypeCommand string = "command"
|
MsgTypeCommand string = "command"
|
||||||
MsgTypeOrder string = "order"
|
MsgTypeOrder string = "order"
|
||||||
MsgTypeProduct string = "product"
|
MsgTypeProduct string = "product"
|
||||||
|
|
||||||
|
MsgOrderStatusCodeNew = "new"
|
||||||
|
MsgOrderStatusCodeApproval = "approval"
|
||||||
|
MsgOrderStatusCodeAssembling = "assembling"
|
||||||
|
MsgOrderStatusCodeDelivery = "delivery"
|
||||||
|
MsgOrderStatusCodeComplete = "complete"
|
||||||
|
MsgOrderStatusCodeCancel = "cancel"
|
||||||
|
|
||||||
|
MsgCurrencyRub = "rub"
|
||||||
|
MsgCurrencyUah = "uah"
|
||||||
|
MsgCurrencyByr = "byr"
|
||||||
|
MsgCurrencyKzt = "kzt"
|
||||||
|
MsgCurrencyUsd = "usd"
|
||||||
|
MsgCurrencyEur = "eur"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MgClient type
|
// MgClient type
|
||||||
@ -366,12 +380,14 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
MessageOrder struct {
|
MessageOrder struct {
|
||||||
Number string `json:"number"`
|
Number string `json:"number"`
|
||||||
Url string `json:"url,omitempty"`
|
Url string `json:"url,omitempty"`
|
||||||
Date string `json:"date,omitempty"`
|
Date string `json:"date,omitempty"`
|
||||||
Cost *MessageOrderCost `json:"cost,omitempty"`
|
Cost *MessageOrderCost `json:"cost,omitempty"`
|
||||||
Status *MessageOrderStatus `json:"status,omitempty"`
|
Status *MessageOrderStatus `json:"status,omitempty"`
|
||||||
Items []MessageOrderItem `json:"items,omitempty"`
|
Delivery *MessageOrderDelivery `json:"delivery"`
|
||||||
|
Payments []MessageOrderPayment `json:"payment"`
|
||||||
|
Items []MessageOrderItem `json:"items,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageOrderStatus struct {
|
MessageOrderStatus struct {
|
||||||
@ -396,6 +412,23 @@ type (
|
|||||||
Unit string `json:"unit"`
|
Unit string `json:"unit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MessageOrderPayment struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Status *MessageOrderPaymentStatus `json:"status"`
|
||||||
|
Amount *MessageOrderCost `json:"amount"`
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageOrderPaymentStatus struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Payed bool `json:"payed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageOrderDelivery struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Amount *MessageOrderCost `json:"amount"`
|
||||||
|
Address string `json:"address"`
|
||||||
|
}
|
||||||
|
|
||||||
UserRef struct {
|
UserRef struct {
|
||||||
ID uint64 `json:"id"`
|
ID uint64 `json:"id"`
|
||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
|
Loading…
Reference in New Issue
Block a user