Skip to content

Commit

Permalink
feat: add all event structures
Browse files Browse the repository at this point in the history
Signed-off-by: sarthakjdev <[email protected]>
  • Loading branch information
sarthakjdev committed Jun 1, 2024
1 parent af9bf60 commit 82a3fda
Show file tree
Hide file tree
Showing 48 changed files with 513 additions and 203 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
node_modules
.vscode/
config.toml
node_modules
dist/*
76 changes: 0 additions & 76 deletions a.json

This file was deleted.

17 changes: 16 additions & 1 deletion example-chat-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ func main() {

})

whatsappClient.InitiateClient()
whatsappClient.On(manager.AudioMessageEvent, func(be events.BaseEvent) {
fmt.Println("audio message event received")
})

whatsappClient.On(manager.VideoMessageEvent, func(be events.BaseEvent) {
fmt.Println("video message event received")
})

whatsappClient.On(manager.DocumentMessageEvent, func(be events.BaseEvent) {
fmt.Println("document message event received")
})

whatsappClient.On(manager.ImageMessageEvent, func(be events.BaseEvent) {
fmt.Println("image message event received")
})

whatsappClient.InitiateClient()
}
21 changes: 14 additions & 7 deletions internal/manager/event_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ import (
"github.com/sarthakjdev/wapi.go/pkg/events"
)

// ChannelEvent represents an event that can be published and subscribed to.
type ChannelEvent struct {
Type EventType
Data events.BaseEvent
Type EventType // Type is the type of the event.
Data events.BaseEvent // Data is the data associated with the event.
}

// EventManger is responsible for managing events and their subscribers.
type EventManger struct {
subscribers map[EventType]chan ChannelEvent
sync.RWMutex
subscribers map[EventType]chan ChannelEvent // subscribers is a map of event types to channels of ChannelEvent.
sync.RWMutex // RWMutex is used to synchronize access to the subscribers map.
}

// NewEventManager creates a new instance of EventManger.
func NewEventManager() *EventManger {
return &EventManger{
subscribers: make(map[EventType]chan ChannelEvent),
}
}

// subscriber to this event listener will be notified when the event is published
// Subscribe adds a new subscriber to the specified event type.
// The subscriber will be notified when the event is published.
func (em *EventManger) Subscribe(eventName EventType) (chan ChannelEvent, error) {
em.Lock()
defer em.Unlock()
Expand All @@ -34,14 +38,14 @@ func (em *EventManger) Subscribe(eventName EventType) (chan ChannelEvent, error)
return em.subscribers[eventName], nil
}

// subscriber to this event listener will be notified when the event is published
// Unsubscribe removes a subscriber from the specified event type.
func (em *EventManger) Unsubscribe(id EventType) {
em.Lock()
defer em.Unlock()
delete(em.subscribers, id)
}

// publish event to this events system and let all the subscriber consume them
// Publish publishes an event to the event system and notifies all the subscribers.
func (em *EventManger) Publish(eventType EventType, data events.BaseEvent) error {
fmt.Println("Publishing event: ", eventType)
em.Lock()
Expand All @@ -60,6 +64,9 @@ func (em *EventManger) Publish(eventType EventType, data events.BaseEvent) error
return nil
}

// On registers a handler function for the specified event type.
// The handler function will be called whenever the event is published.
// It returns the event type that the handler is registered for.
func (em *EventManger) On(eventName EventType, handler func(events.BaseEvent)) EventType {
ch, _ := em.Subscribe(eventName)
go func() {
Expand Down
8 changes: 4 additions & 4 deletions internal/manager/media_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package manager

import requestclient "github.com/sarthakjdev/wapi.go/internal/request_client"

// MediaManager is responsible for managing media related operations.
type MediaManager struct {
requester requestclient.RequestClient
}

// NewMediaManager creates a new instance of MediaManager.
func NewMediaManager(requester requestclient.RequestClient) *MediaManager {
return &MediaManager{
requester: requester,
}
}

func (mm *MediaManager) UploadMedia() {
// upload media
}

// GetMediaUrlById retrieves the media URL by its ID.
func (mm *MediaManager) GetMediaUrlById() {

}

// GetMediaIdByUrl retrieves the media ID by its URL.
func (mm *MediaManager) GetMediaIdByUrl() {

}
9 changes: 7 additions & 2 deletions internal/manager/message_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,33 @@ import (
"github.com/sarthakjdev/wapi.go/pkg/components"
)

// MessageManager is responsible for managing messages.
type MessageManager struct {
requester requestclient.RequestClient
}

// NewMessageManager creates a new instance of MessageManager.
func NewMessageManager(requester requestclient.RequestClient) *MessageManager {
return &MessageManager{
requester: requester,
}
}

// SendMessageParams represents the parameters for sending a message.
type SendMessageParams struct {
Message components.BaseMessage
PhoneNumber string
}

// ! TODO: return the structured response from here
// Send sends a message with the given parameters and returns the response.
// TODO: return the structured response from here
func (mm *MessageManager) Send(params SendMessageParams) (string, error) {
body, err := params.Message.ToJson(components.ApiCompatibleJsonConverterConfigs{
SendToPhoneNumber: params.PhoneNumber,
// ReplyToMessageId: "wamid.HBgMOTE5NjQzNTAwNTQ1FQIAERgSQzVGOTlFMzExQ0VCQTg0MUFCAA==",
})
if err != nil {
// ! TODO: emit a error event here
// TODO: emit an error event here
return "", fmt.Errorf("error converting message to json: %v", err)
}
mm.requester.RequestCloudApi(requestclient.RequestCloudApiParams{
Expand All @@ -39,6 +43,7 @@ func (mm *MessageManager) Send(params SendMessageParams) (string, error) {
return "ok", nil
}

// Reply replies to a message.
func (mm *MessageManager) Reply() {
// Reply to message
}
13 changes: 0 additions & 13 deletions internal/manager/phone_manager.go

This file was deleted.

Loading

0 comments on commit 82a3fda

Please sign in to comment.