-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 037f0d1
Showing
11 changed files
with
493 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# This workflow will build a golang project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | ||
|
||
name: Go | ||
|
||
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.22.1' | ||
|
||
- name: Test | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 toanppp | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# zgo | ||
|
||
Zalo Go APIs | ||
|
||
## To do | ||
|
||
- [x] [Authenticate](https://developers.zalo.me/docs/official-account/bat-dau/xac-thuc-va-uy-quyen-cho-ung-dung-new) | ||
- [ ] [Message](https://developers.zalo.me/docs/official-account/tin-nhan/tong-quan) | ||
- [x] Get messages in a conversation | ||
- [ ] [Webhook](https://developers.zalo.me/docs/official-account/webhook/tong-quan) | ||
- [x] Calculate signature | ||
|
||
## Install | ||
|
||
```sh | ||
go get github.com/toanppp/zgo | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package zgo | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
type GetAccessTokenResp struct { | ||
AccessToken string `json:"access_token"` | ||
RefreshToken string `json:"refresh_token"` | ||
ExpiresIn string `json:"expires_in"` | ||
ErrorResp | ||
} | ||
|
||
func (z *zgo) RefreshAccessToken(ctx context.Context, refreshToken string) (GetAccessTokenResp, error) { | ||
payload := url.Values{} | ||
payload.Set("app_id", z.appID) | ||
payload.Set("grant_type", "refresh_token") | ||
payload.Set("refresh_token", refreshToken) | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, getAccessTokenURL, bytes.NewBufferString(payload.Encode())) | ||
if err != nil { | ||
return GetAccessTokenResp{}, err | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
req.Header.Set("secret_key", z.appSecret) | ||
|
||
resp, err := z.httpClient.Do(req) | ||
if err != nil { | ||
return GetAccessTokenResp{}, err | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return GetAccessTokenResp{}, errors.New("request failed") | ||
} | ||
|
||
responseBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return GetAccessTokenResp{}, err | ||
} | ||
|
||
var data GetAccessTokenResp | ||
if err := json.Unmarshal(responseBody, &data); err != nil { | ||
return GetAccessTokenResp{}, err | ||
} | ||
|
||
if data.Error != 0 { | ||
return data, fmt.Errorf("request failed: %+v", data) | ||
} | ||
|
||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package zgo | ||
|
||
const ( | ||
TooManyRequest = -32 | ||
InvalidAccessToken = -216 | ||
InvalidRefreshToken = -14014 | ||
ExpiredRefreshToken = -14020 | ||
) | ||
|
||
type ErrorResp struct { | ||
ErrorName string `json:"error_name"` | ||
ErrorReason string `json:"error_reason"` | ||
RefDoc string `json:"ref_doc"` | ||
ErrorDescription string `json:"error_description"` | ||
Error int `json:"error"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package zgo | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const ( | ||
AddUserToTag = "add_user_to_tag" | ||
RemoveUserFromTag = "remove_user_from_tag" | ||
|
||
TagFinished = "Hoàn thành" | ||
) | ||
|
||
type Event struct { | ||
OAID string `json:"oa_id"` | ||
UserIDByApp string `json:"user_id_by_app"` | ||
EventName string `json:"event_name"` | ||
Tag UserTag `json:"tag"` | ||
AppID string `json:"app_id"` | ||
Timestamp string `json:"timestamp"` | ||
} | ||
|
||
func (e Event) CreatedAt() time.Time { | ||
msec, err := strconv.ParseInt(e.Timestamp, 10, 64) | ||
if err != nil { | ||
return time.Time{} | ||
} | ||
return time.UnixMilli(msec) | ||
} | ||
|
||
type UserTag struct { | ||
UserIDs []string `json:"user_ids"` | ||
Name string `json:"name"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/toanppp/zgo | ||
|
||
go 1.22.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package zgo | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
const ( | ||
SrcOA = iota | ||
SrcUser | ||
) | ||
|
||
type GetConversationResp struct { | ||
Data Conversation `json:"data"` | ||
ErrorResp | ||
} | ||
|
||
type Conversation []Message | ||
|
||
func (c Conversation) Profile() Profile { | ||
if len(c) == 0 { | ||
return Profile{} | ||
} | ||
switch c[0].Src { | ||
case SrcOA: | ||
return Profile{ | ||
UserID: c[0].ToID, | ||
DisplayName: c[0].ToDisplayName, | ||
} | ||
case SrcUser: | ||
return Profile{ | ||
UserID: c[0].FromID, | ||
DisplayName: c[0].FromDisplayName, | ||
} | ||
} | ||
return Profile{} | ||
} | ||
|
||
type Profile struct { | ||
UserID string `json:"user_id"` | ||
DisplayName string `json:"display_name"` | ||
} | ||
|
||
type Message struct { | ||
Src int `json:"src" bson:"src"` | ||
Time int64 `json:"time" bson:"time"` | ||
SentTime string `json:"sent_time" bson:"sent_time"` | ||
FromID string `json:"from_id" bson:"from_id"` | ||
FromDisplayName string `json:"from_display_name" bson:"from_display_name"` | ||
FromAvatar string `json:"from_avatar" bson:"from_avatar"` | ||
ToID string `json:"to_id" bson:"to_id"` | ||
ToDisplayName string `json:"to_display_name" bson:"to_display_name"` | ||
ToAvatar string `json:"to_avatar" bson:"to_avatar"` | ||
MessageID string `json:"message_id" bson:"message_id"` | ||
Type string `json:"type" bson:"type"` | ||
Message string `json:"message" bson:"message"` | ||
Links []Link `json:"links,omitempty" bson:"links"` | ||
Thumb string `json:"thumb" bson:"thumb"` | ||
URL string `json:"url" bson:"url"` | ||
Description string `json:"description" bson:"description"` | ||
} | ||
|
||
type Link struct { | ||
Title string `json:"title" bson:"title"` | ||
URL string `json:"url" bson:"url"` | ||
Thumb string `json:"thumb" bson:"thumb"` | ||
Description string `json:"description" bson:"description"` | ||
} | ||
|
||
type LinkDescription struct { | ||
Caption string `json:"caption"` | ||
Phone string `json:"phone"` | ||
} | ||
|
||
func (l Link) String() string { | ||
var data LinkDescription | ||
if err := json.Unmarshal([]byte(l.Description), &data); err != nil { | ||
return l.URL | ||
} | ||
|
||
switch l.URL { | ||
case "https://zalo.me": | ||
return data.Caption | ||
case "www.zaloapp.com": | ||
return fmt.Sprintf("Danh thiếp: %s - %s", l.Title, data.Phone) | ||
default: | ||
return l.URL | ||
} | ||
} | ||
|
||
type GetConversationReq struct { | ||
UserID int64 `json:"user_id"` | ||
Offset int `json:"offset"` | ||
Count int `json:"count"` | ||
} | ||
|
||
func (z *zgo) GetConversation(ctx context.Context, accessToken string, reqData GetConversationReq) (GetConversationResp, error) { | ||
u, err := url.Parse(getConversationURL) | ||
if err != nil { | ||
return GetConversationResp{}, err | ||
} | ||
|
||
bytes, err := json.Marshal(reqData) | ||
if err != nil { | ||
return GetConversationResp{}, err | ||
} | ||
params := url.Values{} | ||
params.Set("data", string(bytes)) | ||
u.RawQuery = params.Encode() | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) | ||
if err != nil { | ||
return GetConversationResp{}, err | ||
} | ||
|
||
req.Header.Set("access_token", accessToken) | ||
|
||
resp, err := z.httpClient.Do(req) | ||
if err != nil { | ||
return GetConversationResp{}, err | ||
} | ||
defer func() { _ = resp.Body.Close() }() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return GetConversationResp{}, errors.New("request failed") | ||
} | ||
|
||
responseBody, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return GetConversationResp{}, err | ||
} | ||
|
||
var data GetConversationResp | ||
if err := json.Unmarshal(responseBody, &data); err != nil { | ||
return GetConversationResp{}, err | ||
} | ||
|
||
if data.Error != 0 { | ||
return data, fmt.Errorf("request failed: %+v", data) | ||
} | ||
|
||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package zgo_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/toanppp/zgo" | ||
) | ||
|
||
func TestLink_String(t *testing.T) { | ||
type fields struct { | ||
Title string | ||
URL string | ||
Thumb string | ||
Description string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want string | ||
}{ | ||
{ | ||
name: "link", | ||
fields: fields{ | ||
URL: "https://google.com", | ||
}, | ||
want: "https://google.com", | ||
}, | ||
{ | ||
name: "number", | ||
fields: fields{ | ||
Title: "Name", | ||
URL: "https://zalo.me", | ||
Description: "{\"phone\":\"767263039\",\"caption\":\"767263039\",\"qrCodeUrl\":\"https:\\/\\/qr-talk.zdn.vn\\/9\\/77327221\\/b7b2f11d85566c083547.jpg\"}", | ||
}, | ||
want: "767263039", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
l := zgo.Link{ | ||
Title: tt.fields.Title, | ||
URL: tt.fields.URL, | ||
Thumb: tt.fields.Thumb, | ||
Description: tt.fields.Description, | ||
} | ||
if got := l.String(); got != tt.want { | ||
t.Errorf("String() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.