github.com/grokify/go-ringcentral-client@v0.3.31/office/v1/util/event.go (about)

     1  package clientutil
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"time"
    11  
    12  	rc "github.com/grokify/go-ringcentral-client/office/v1/client"
    13  )
    14  
    15  const (
    16  	ThisDir = "src/github.com/grokify/go-ringcentral-client/office/v1/client"
    17  )
    18  
    19  // EventSimple is a event for Instant SMS for testing purposes
    20  type EventSimple struct {
    21  	UUID           string    `json:"uuid,omitempty"`
    22  	Event          string    `json:"event,omitempty"`
    23  	Timestamp      time.Time `json:"timestamp,omitempty"`
    24  	SubscriptionId string    `json:"subscriptionId,omitempty"`
    25  	OwnerId        string    `json:"ownerId,omitempty"`
    26  	Body           EventBody `json:"body,omitempty"`
    27  }
    28  
    29  // Event is a generic event.
    30  type Event struct {
    31  	UUID           string           `json:"uuid,omitempty"`
    32  	Event          string           `json:"event,omitempty"`
    33  	Timestamp      time.Time        `json:"timestamp,omitempty"`
    34  	SubscriptionId string           `json:"subscriptionId,omitempty"`
    35  	OwnerId        string           `json:"ownerId,omitempty"`
    36  	Body           EventBodyWrapper `json:"body,omitempty"`
    37  }
    38  
    39  func (evt *Event) IsEventType(eventType EventType) bool {
    40  	parsedEvtType, err := ParseEventTypeForFilter(evt.Event)
    41  	if err != nil {
    42  		return false
    43  	}
    44  	if eventType == parsedEvtType {
    45  		fmt.Println(evt.Event)
    46  		fmt.Println(parsedEvtType.String())
    47  		//panic("AA")
    48  		return true
    49  	}
    50  	return false
    51  }
    52  
    53  type EventBodyWrapper struct {
    54  	Raw    string      `json:"raw"`
    55  	Parsed interface{} `json:"parsed"`
    56  }
    57  
    58  func (w *EventBodyWrapper) UnmarshalJSON(data []byte) error {
    59  	w.Raw = string(data)
    60  	return nil
    61  }
    62  
    63  func EventParseBytes(data []byte) (*Event, error) {
    64  	evt := &Event{}
    65  	err := json.Unmarshal(data, evt)
    66  	return evt, err
    67  }
    68  
    69  func (evt *Event) GetInstantMessageBody() (*rc.InstantMessageEvent, error) {
    70  	evtType, err := ParseEventTypeForFilter(evt.Event)
    71  	if err != nil {
    72  		return nil, fmt.Errorf("Cannot parse event: %v", evt.Event)
    73  	} else if evtType != InstantMessageEvent {
    74  		return nil, fmt.Errorf("Incorrect event type: %v", evtType.String())
    75  	}
    76  	body := &rc.InstantMessageEvent{}
    77  	err = json.Unmarshal([]byte(evt.Body.Raw), body)
    78  	return body, err
    79  }
    80  
    81  func (evt *Event) GetGlipPostEventBody() (*rc.GlipPostEvent, error) {
    82  	evtType, err := ParseEventTypeForFilter(evt.Event)
    83  	if err != nil {
    84  		return nil, fmt.Errorf("Cannot parse event: %v", evt.Event)
    85  	} else if evtType != GlipPostEvent {
    86  		return nil, fmt.Errorf("Incorrect event type: %v", evtType.String())
    87  	}
    88  	body := &rc.GlipPostEvent{}
    89  	err = json.Unmarshal([]byte(evt.Body.Raw), body)
    90  	return body, err
    91  }
    92  
    93  func GetFileBytesForEventType(eventType string) ([]byte, error) {
    94  	file := GetFilePathForEventType(eventType)
    95  	return ioutil.ReadFile(file)
    96  }
    97  
    98  func GetFilePathForEventType(eventType string) string {
    99  	filename := GetFileNameForEventType(eventType)
   100  	return filepath.Join(os.Getenv("GOPATH"), ThisDir, filename)
   101  }
   102  
   103  func GetFileNameForEventType(eventType string) string {
   104  	return fmt.Sprintf("example_event_%s.json", strings.ToLower(eventType))
   105  }
   106  
   107  type EventBody struct {
   108  	ExpiresIn            int       `json:"expiresIn,omitempty"`
   109  	CreationTime         time.Time `json:"creationTime,omitempty"`
   110  	Direction            string    `json:"direction,omitempty"`
   111  	LastModificationTime time.Time `json:"lastModificationTime,omitempty"`
   112  	Subject              string    `json:"subject,omitempty"`
   113  	Type                 string    `json:"type,omitempty"`
   114  }
   115  
   116  /*
   117   * RingCentral Connect Platform API Explorer
   118   *
   119   * <p>This is a beta interactive API explorer for the RingCentral Connect Platform. To use this service, you will need to have an account with the proper credentials to generate an OAuth2 access token.</p><p><h2>Quick Start</h2></p><ol><li>1) Go to <b>Authentication > /oauth/token</b></li><li>2) Enter <b>app_key, app_secret, username, password</b> fields and then click \"Try it out!\"</li><li>3) Upon success, your access_token is loaded and you can access any form requiring authorization.</li></ol><h2>Links</h2><ul><li><a href=\"https://github.com/ringcentral\" target=\"_blank\">RingCentral SDKs on Github</a></li><li><a href=\"mailto:devsupport@ringcentral.com\">RingCentral Developer Support Email</a></li></ul>
   120   *
   121   * API version: 1.0
   122   * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
   123   */
   124  /*
   125  type InstantMessageEvent struct {
   126  
   127  	// Internal identifier of an message
   128  	Id string `json:"id,omitempty"`
   129  
   130  	// Message receiver(s) information
   131  	To []NotificationRecipientInfo `json:"to,omitempty"`
   132  
   133  	// Extension Type
   134  	From *SenderInfo `json:"from,omitempty"`
   135  
   136  	// Type of a message. The default value is 'SMS'
   137  	Type_ string `json:"type,omitempty"`
   138  
   139  	// Message creation datetime in ISO 8601 format including timezone, for example 2016-03-10T18:07:52.534Z
   140  	CreationTime time.Time `json:"creationTime,omitempty"`
   141  
   142  	// Datetime when the message was modified in ISO 8601 format including timezone, for example 2016-03-10T18:07:52.534Z
   143  	LastModifiedTime time.Time `json:"lastModifiedTime,omitempty"`
   144  
   145  	// Status of a message. The default value is 'Unread'
   146  	ReadStatus string `json:"readStatus,omitempty"`
   147  
   148  	// The default value is 'Normal'
   149  	Priority string `json:"priority,omitempty"`
   150  
   151  	// Message attachment data
   152  	Attachments []MessageAttachmentInfo `json:"attachments,omitempty"`
   153  
   154  	// Message direction. The default value is 'Inbound'
   155  	Direction string `json:"direction,omitempty"`
   156  
   157  	// Message availability status. The default value is 'Alive'
   158  	Availability string `json:"availability,omitempty"`
   159  
   160  	// Message subject. It replicates message text which is also returned as an attachment
   161  	Subject string `json:"subject,omitempty"`
   162  
   163  	// Status of a message. The default value is 'Received'
   164  	MessageStatus string `json:"messageStatus,omitempty"`
   165  
   166  	// Identifier of the conversation the message belongs to
   167  	ConversationId string `json:"conversationId,omitempty"`
   168  }
   169  */