-
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.
feat(freshchat): user, agent, conversation
- Loading branch information
0 parents
commit 13223bc
Showing
12 changed files
with
774 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,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,63 @@ | ||
# freshgo | ||
|
||
Freshworks Golang APIs | ||
|
||
# To do | ||
|
||
- [x] [Freshchat](https://developers.freshchat.com/) | ||
- [x] User | ||
- [x] Agent | ||
- [x] Conversation | ||
- [ ] Freshdesk | ||
|
||
# Install | ||
|
||
```sh | ||
go get github.com/toanppp/freshgo | ||
``` | ||
|
||
# Example | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/toanppp/freshgo/freshchat" | ||
) | ||
|
||
func main() { | ||
f := freshchat.New("url", "accessToken") | ||
|
||
resp, err := f.CreateConversation(context.Background(), freshchat.Conversation{ | ||
Status: freshchat.ConversationStatusNew, | ||
Messages: []freshchat.Message{ | ||
{ | ||
MessageParts: []freshchat.MessagePart{ | ||
{ | ||
Text: freshchat.Text{ | ||
Content: "Hello World", | ||
}, | ||
}, | ||
}, | ||
ActorType: freshchat.ActorTypeUser, | ||
ActorID: "userID", | ||
}, | ||
}, | ||
ChannelID: "channelID", | ||
Users: []freshchat.User{ | ||
{ | ||
ID: "userID", | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Fatalf("an error occurred: %v", err) | ||
} | ||
|
||
fmt.Printf("%+v\n", resp) | ||
} | ||
``` |
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,79 @@ | ||
package freshchat | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
type Agent struct { | ||
ID string `json:"id"` | ||
CreatedTime time.Time `json:"created_time"` | ||
AgentCapacity int `json:"agent_capacity"` | ||
AgentStatus AgentStatus `json:"agent_status"` | ||
AvailabilityStatus string `json:"availability_status"` | ||
Avatar Avatar `json:"avatar"` | ||
Biography string `json:"biography"` | ||
Email string `json:"email"` | ||
FirstName string `json:"first_name"` | ||
FreshidGroupDs any `json:"freshid_group_ids"` | ||
FreshidUuid string `json:"freshid_uuid"` | ||
Groups []string `json:"groups"` | ||
IsDeactivated bool `json:"is_deactivated"` | ||
IsDeleted bool `json:"is_deleted"` | ||
LastName string `json:"last_name"` | ||
LicenseType string `json:"license_type"` | ||
Locale string `json:"locale"` | ||
LoginStatus bool `json:"login_status"` | ||
OrgContactID string `json:"org_contact_id"` | ||
RoleID string `json:"role_id"` | ||
RoleName string `json:"role_name"` | ||
RoutingType string `json:"routing_type"` | ||
SkillID string `json:"skill_id"` | ||
SocialProfiles []SocialProfile `json:"social_profiles"` | ||
Timezone string `json:"timezone"` | ||
} | ||
|
||
type AgentStatus struct { | ||
Name string `json:"name"` | ||
} | ||
|
||
type SocialProfile struct { | ||
Type string `json:"type"` | ||
ID string `json:"id"` | ||
} | ||
|
||
func (f *freshchat) GetAgentInfo(ctx context.Context, agentID string) (Agent, error) { | ||
u, _ := url.Parse(fmt.Sprintf("%s/%s", f.url, fmt.Sprintf(pathAgent, agentID))) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) | ||
if err != nil { | ||
return Agent{}, fmt.Errorf("http.NewRequestWithContext: %w", err) | ||
} | ||
req.Header.Add("Authorization", f.accessToken) | ||
|
||
resp, err := f.httpClient.Do(req) | ||
if err != nil { | ||
return Agent{}, fmt.Errorf("httpClient.Do: %w", err) | ||
} | ||
|
||
if resp.StatusCode < http.StatusOK || http.StatusMultipleChoices <= resp.StatusCode { | ||
p, _ := io.ReadAll(resp.Body) | ||
return Agent{}, fmt.Errorf("request failed: %s", string(p)) | ||
} | ||
|
||
p, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return Agent{}, fmt.Errorf("io.ReadAll: %w", err) | ||
} | ||
|
||
var output Agent | ||
if err := json.Unmarshal(p, &output); err != nil { | ||
return Agent{}, fmt.Errorf("json.Unmarshal: %w", err) | ||
} | ||
|
||
return output, 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,21 @@ | ||
package freshchat_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/toanppp/freshgo/freshchat" | ||
) | ||
|
||
func TestFreshchat_GetAgentInfo(t *testing.T) { | ||
f := freshchat.New(url, accessToken) | ||
resp, err := f.GetAgentInfo(context.Background(), agentID) | ||
if err != nil { | ||
t.Fatalf("an error occurred: %v", err) | ||
} | ||
if resp.ID != agentID { | ||
t.Errorf("invalid agent_id: %+v", resp) | ||
} | ||
fmt.Printf("%+v\n", resp) | ||
} |
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,207 @@ | ||
package freshchat | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
const ( | ||
ConversationStatusNew = "new" | ||
ConversationStatusAssigned = "assigned" | ||
ConversationStatusResolved = "resolved" | ||
ConversationStatusReopened = "reopened" | ||
|
||
ActorTypeUser = "user" | ||
) | ||
|
||
type Conversation struct { | ||
ConversationID string `json:"conversation_id,omitempty"` | ||
ChannelID string `json:"channel_id,omitempty"` | ||
AssignedOrgAgentID string `json:"assigned_org_agent_id,omitempty"` | ||
AssignedAgentID string `json:"assigned_agent_id,omitempty"` | ||
AssignedOrgGroupID string `json:"assigned_org_group_id,omitempty"` | ||
AssignedGroupID string `json:"assigned_group_id,omitempty"` | ||
Messages []Message `json:"messages,omitempty"` | ||
AppID string `json:"app_id,omitempty"` | ||
Status string `json:"status,omitempty"` | ||
SkillID int64 `json:"skill_id,omitempty"` | ||
Properties Properties `json:"properties,omitempty"` | ||
Users []User `json:"users,omitempty"` | ||
} | ||
|
||
type Message struct { | ||
MessageParts []MessagePart `json:"message_parts,omitempty"` | ||
AppID string `json:"app_id,omitempty"` | ||
ActorID string `json:"actor_id,omitempty"` | ||
OrgActorID string `json:"org_actor_id,omitempty"` | ||
ID string `json:"id,omitempty"` | ||
ChannelID string `json:"channel_id,omitempty"` | ||
ConversationID string `json:"conversation_id,omitempty"` | ||
InteractionID string `json:"interaction_id,omitempty"` | ||
MessageType string `json:"message_type,omitempty"` | ||
ActorType string `json:"actor_type,omitempty"` | ||
CreatedTime string `json:"created_time,omitempty"` | ||
UserID string `json:"user_id,omitempty"` | ||
RestrictResponse bool `json:"restrictResponse,omitempty"` | ||
BotsPrivateNote bool `json:"botsPrivateNote,omitempty"` | ||
} | ||
|
||
type MessagePart struct { | ||
Text Text `json:"text,omitempty"` | ||
} | ||
|
||
type Text struct { | ||
Content string `json:"content,omitempty"` | ||
} | ||
|
||
type Properties struct { | ||
Priority string `json:"priority,omitempty"` | ||
CFType string `json:"cf_type,omitempty"` | ||
CFRating string `json:"cf_rating,omitempty"` | ||
} | ||
|
||
type MessagesResp struct { | ||
Messages []Message `json:"messages,omitempty"` | ||
} | ||
|
||
func (f *freshchat) CreateConversation(ctx context.Context, input Conversation) (Conversation, error) { | ||
url := fmt.Sprintf("%s/%s", f.url, pathConversations) | ||
pReq, _ := json.Marshal(input) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(pReq)) | ||
if err != nil { | ||
return Conversation{}, fmt.Errorf("http.NewRequestWithContext: %w", err) | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
req.Header.Add("Accept", "application/json") | ||
req.Header.Add("Authorization", f.accessToken) | ||
|
||
resp, err := f.httpClient.Do(req) | ||
if err != nil { | ||
return Conversation{}, fmt.Errorf("httpClient.Do: %w", err) | ||
} | ||
|
||
if resp.StatusCode < http.StatusOK || http.StatusMultipleChoices <= resp.StatusCode { | ||
pResp, _ := io.ReadAll(resp.Body) | ||
return Conversation{}, fmt.Errorf("request failed: %s", string(pResp)) | ||
} | ||
|
||
pResp, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return Conversation{}, fmt.Errorf("io.ReadAll: %w", err) | ||
} | ||
|
||
var output Conversation | ||
if err := json.Unmarshal(pResp, &output); err != nil { | ||
return Conversation{}, fmt.Errorf("json.Unmarshal: %w", err) | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
func (f *freshchat) UpdateConversation(ctx context.Context, conversationID string, input Conversation) (Conversation, error) { | ||
url := fmt.Sprintf("%s/%s", f.url, fmt.Sprintf(pathConversation, conversationID)) | ||
pReq, _ := json.Marshal(input) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewBuffer(pReq)) | ||
if err != nil { | ||
return Conversation{}, fmt.Errorf("http.NewRequestWithContext: %w", err) | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
req.Header.Add("Accept", "application/json") | ||
req.Header.Add("Authorization", f.accessToken) | ||
|
||
resp, err := f.httpClient.Do(req) | ||
if err != nil { | ||
return Conversation{}, fmt.Errorf("httpClient.Do: %w", err) | ||
} | ||
|
||
if resp.StatusCode < http.StatusOK || http.StatusMultipleChoices <= resp.StatusCode { | ||
pResp, _ := io.ReadAll(resp.Body) | ||
return Conversation{}, fmt.Errorf("request failed: %s", string(pResp)) | ||
} | ||
|
||
pResp, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return Conversation{}, fmt.Errorf("io.ReadAll: %w", err) | ||
} | ||
|
||
var output Conversation | ||
if err := json.Unmarshal(pResp, &output); err != nil { | ||
return Conversation{}, fmt.Errorf("json.Unmarshal: %w", err) | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
func (f *freshchat) ListMessages(ctx context.Context, conversationID string, input ListReq) (MessagesResp, error) { | ||
u, _ := url.Parse(fmt.Sprintf("%s/%s", f.url, fmt.Sprintf(pathMessages, conversationID))) | ||
u.RawQuery = input.Values().Encode() | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) | ||
if err != nil { | ||
return MessagesResp{}, fmt.Errorf("http.NewRequestWithContext: %w", err) | ||
} | ||
req.Header.Add("Authorization", f.accessToken) | ||
|
||
resp, err := f.httpClient.Do(req) | ||
if err != nil { | ||
return MessagesResp{}, fmt.Errorf("httpClient.Do: %w", err) | ||
} | ||
|
||
if resp.StatusCode < http.StatusOK || http.StatusMultipleChoices <= resp.StatusCode { | ||
p, _ := io.ReadAll(resp.Body) | ||
return MessagesResp{}, fmt.Errorf("request failed: %s", string(p)) | ||
} | ||
|
||
p, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return MessagesResp{}, fmt.Errorf("io.ReadAll: %w", err) | ||
} | ||
|
||
var output MessagesResp | ||
if err := json.Unmarshal(p, &output); err != nil { | ||
return MessagesResp{}, fmt.Errorf("json.Unmarshal: %w", err) | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
func (f *freshchat) SendMessage(ctx context.Context, conversationID string, input Message) (Message, error) { | ||
url := fmt.Sprintf("%s/%s", f.url, fmt.Sprintf(pathMessages, conversationID)) | ||
pReq, _ := json.Marshal(input) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(pReq)) | ||
if err != nil { | ||
return Message{}, fmt.Errorf("http.NewRequestWithContext: %w", err) | ||
} | ||
|
||
req.Header.Add("Content-Type", "application/json") | ||
req.Header.Add("Accept", "application/json") | ||
req.Header.Add("Authorization", f.accessToken) | ||
|
||
resp, err := f.httpClient.Do(req) | ||
if err != nil { | ||
return Message{}, fmt.Errorf("httpClient.Do: %w", err) | ||
} | ||
|
||
if resp.StatusCode < http.StatusOK || http.StatusMultipleChoices <= resp.StatusCode { | ||
pResp, _ := io.ReadAll(resp.Body) | ||
return Message{}, fmt.Errorf("request failed: %s", string(pResp)) | ||
} | ||
|
||
pResp, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return Message{}, fmt.Errorf("io.ReadAll: %w", err) | ||
} | ||
|
||
var output Message | ||
if err := json.Unmarshal(pResp, &output); err != nil { | ||
return Message{}, fmt.Errorf("json.Unmarshal: %w", err) | ||
} | ||
|
||
return output, nil | ||
} |
Oops, something went wrong.