Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetSenderInfo function to get message sender info #58

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions connector/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
Expand All @@ -40,6 +41,7 @@ import (
type Client interface {
Post(url url.URL, activity schema.Activity) error
Delete(url url.URL, activity schema.Activity) error
Get(url url.URL) ([]byte, error)
}

// ConnectorClient implements Client to send HTTP requests to the connector service.
Expand All @@ -58,6 +60,37 @@ func NewClient(config *Config) (Client, error) {
return &ConnectorClient{*config, cache.AuthCache{}}, nil
}

// Get fetches data from given URL.
func (client *ConnectorClient) Get(target url.URL) ([]byte, error) {

token, err := client.getToken()
if err != nil {
return nil, err
}

req, err := http.NewRequest("GET", target.String(), nil)
if err != nil {
return nil, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)

httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return body, nil
Comment on lines +76 to +91
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use client.sendRequest() method to avoid duplicate code?

}

// Post an activity to given URL.
//
// Creates a HTTP POST request with the provided activity as the body and a Bearer token in the header.
Expand Down
38 changes: 38 additions & 0 deletions core/activity/get_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package activity

import (
"encoding/json"
"fmt"
"net/url"
"path"

"github.com/infracloudio/msbotbuilder-go/schema"
"github.com/pkg/errors"
)

const getMemberInfo = "/%s/conversations/%s/members/%s"

// GetSenderInfo get conversation member info.
func (response *DefaultResponse) GetSenderInfo(activity schema.Activity) (*schema.ConversationMember, error) {
u, err := url.Parse(activity.ServiceURL)
if err != nil {
return nil, errors.Wrapf(err, "Failed to parse ServiceURL %s.", activity.ServiceURL)
}

urlString := fmt.Sprintf(getMemberInfo, APIVersion, activity.Conversation.ID, activity.From.ID)

u.Path = path.Join(u.Path, urlString)
raw, err := response.Client.Get(*u)
if err != nil {
return nil, errors.Wrap(err, "Failed to get response.")
}

var convMember schema.ConversationMember

err = json.Unmarshal(raw, &convMember)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse response.")
}

return &convMember, nil
}
1 change: 1 addition & 0 deletions core/activity/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
type Response interface {
SendActivity(activity schema.Activity) error
DeleteActivity(activity schema.Activity) error
GetSenderInfo(activity schema.Activity) (*schema.ConversationMember, error)
}

const (
Expand Down
17 changes: 17 additions & 0 deletions core/bot_framework_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
// Adapter is the primary interface for the user program to perform operations with
// the connector service.
type Adapter interface {
GetSenderInfo(ctx context.Context, req schema.Activity) (*schema.ConversationMember, error)
ParseRequest(ctx context.Context, req *http.Request) (schema.Activity, error)
ProcessActivity(ctx context.Context, req schema.Activity, handler activity.Handler) error
ProactiveMessage(ctx context.Context, ref schema.ConversationReference, handler activity.Handler) error
Expand Down Expand Up @@ -84,6 +85,22 @@ func NewBotAdapter(settings AdapterSetting) (Adapter, error) {
return &BotFrameworkAdapter{settings, auth.NewJwtTokenValidator(), connectorClient}, nil
}

// GetSenderInfo returns information about message sender.
func (bf *BotFrameworkAdapter) GetSenderInfo(ctx context.Context, req schema.Activity) (*schema.ConversationMember, error) {
response, err := activity.NewActivityResponse(bf.Client)
if err != nil {
return nil, errors.Wrap(err, "Failed to create response object.")
}

resp, err := response.GetSenderInfo(req)
if err != nil {
return nil, errors.Wrap(err, "Failed to get data.")
}

return resp, nil

Comment on lines +99 to +101
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return resp, nil
return resp, nil

[nit] unnecessary new lines

}

// ProcessActivity receives an activity, processes it as specified in by the 'handler' and
// sends it to the connector service.
func (bf *BotFrameworkAdapter) ProcessActivity(ctx context.Context, req schema.Activity, handler activity.Handler) error {
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ github.com/pkg/errors v0.9.0 h1:J8lpUdobwIeCI7OiSxHqEwJUKvJwicL5+3v1oe2Yb4k=
github.com/pkg/errors v0.9.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
32 changes: 32 additions & 0 deletions schema/model_conversation_member.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package schema

// ConversationMember - Conversation member info
type ConversationMember struct {

// Member ID
ID string `json:"id,omitempty"`

// Display friendly name
Name string `json:"name,omitempty"`

// GivenName
GivenName string `json:"givenName,omitempty"`

// Surname
Surname string `json:"surname,omitempty"`

// This account's object ID within Azure Active Directory (AAD)
AadObjectID string `json:"aadObjectId,omitempty"`

// Email is user email
Email string `json:"email,omitempty"`

// UserPrincipalName
UserPrincipalName string `json:"userPrincipalName,omitempty"`

// TenantID is an ID fo a tenant
TenantID string `json:"tenantId,omitempty"`

// UserRole is a user role
UserRole RoleTypes `json:"userRole,omitempty"`
}