Skip to content

Commit

Permalink
feat: significant refactoring - moved some interfaces into botinput p…
Browse files Browse the repository at this point in the history
…ackage
  • Loading branch information
trakhimenok committed Aug 30, 2024
1 parent 4735127 commit 710ba6d
Show file tree
Hide file tree
Showing 18 changed files with 587 additions and 476 deletions.
53 changes: 53 additions & 0 deletions botinput/bot_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package botinput

//// BotUser provides info about current bot user
//type BotUser interface {
// // GetBotUserID returns bot user ID
// GetBotUserID() string
//
// // GetFirstName returns user's first name
// GetFirstName() string
//
// // GetLastName returns user's last name
// GetLastName() string
//}
//
//func New(botUserID string, fields ...func(user *botUser)) BotUser {
// svr := &botUser{botUserID: botUserID}
// for _, f := range fields {
// f(svr)
// }
// return svr
//}
//
//var _ BotUser = (*botUser)(nil)
//
//type botUser struct {
// botUserID string
// firstName string
// lastName string
//}
//
//func (v *botUser) GetBotUserID() string {
// return v.botUserID
//}
//
//func (v *botUser) GetFirstName() string {
// return v.firstName
//}
//
//func (v *botUser) GetLastName() string {
// return v.lastName
//}
//
//func WithFirstName(s string) func(user *botUser) {
// return func(v *botUser) {
// v.firstName = s
// }
//}
//
//func WithLastName(s string) func(user *botUser) {
// return func(v *botUser) {
// v.lastName = s
// }
//}
267 changes: 267 additions & 0 deletions botinput/webhook_interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
package botinput

import (
"fmt"
"strconv"
"time"
)

// WebhookEntry represents a single message from a messenger user
type WebhookEntry interface {
GetID() interface{}
GetTime() time.Time
}

// WebhookInputType is enum of input type
type WebhookInputType int

const (
// WebhookInputUnknown is an unknown input type
WebhookInputUnknown WebhookInputType = iota
// WebhookInputNotImplemented is not implemented input type
WebhookInputNotImplemented
// WebhookInputText is a text input type
WebhookInputText // Facebook, Telegram, Viber
// WebhookInputVoice is voice input type
WebhookInputVoice
// WebhookInputPhoto is a photo input type
WebhookInputPhoto
// WebhookInputAudio is an audio input type
WebhookInputAudio
// WebhookInputContact is a contact input type
WebhookInputContact // Facebook, Telegram, Viber
// WebhookInputPostback is unknown input type
WebhookInputPostback
// WebhookInputDelivery is a postback input type
WebhookInputDelivery
// WebhookInputAttachment is a delivery report input type
WebhookInputAttachment
// WebhookInputInlineQuery is an attachment input type
WebhookInputInlineQuery // Telegram
// WebhookInputCallbackQuery is inline input type
WebhookInputCallbackQuery
// WebhookInputReferral is a callback input type
WebhookInputReferral // FBM
// WebhookInputChosenInlineResult is chosen inline result input type
WebhookInputChosenInlineResult // Telegram
// WebhookInputSubscribed is subscribed input type
WebhookInputSubscribed // Viber
// WebhookInputUnsubscribed is unsubscribed input type
WebhookInputUnsubscribed // Viber
// WebhookInputConversationStarted is conversation started input type
WebhookInputConversationStarted // Viber
// WebhookInputNewChatMembers is new botChat members input type
WebhookInputNewChatMembers // Telegram groups
// WebhookInputLeftChatMembers is left botChat members input type
WebhookInputLeftChatMembers
// WebhookInputSticker is a sticker input type
WebhookInputSticker // Telegram
)

var webhookInputTypeNames = map[WebhookInputType]string{
WebhookInputUnknown: "unknown",
WebhookInputNotImplemented: "NotImplemented",
WebhookInputText: "Text",
WebhookInputVoice: "Voice",
WebhookInputPhoto: "Photo",
WebhookInputAudio: "Audio",
WebhookInputReferral: "Referral",
WebhookInputContact: "Contact",
WebhookInputPostback: "Postback",
WebhookInputDelivery: "Delivery",
WebhookInputAttachment: "Attachment",
WebhookInputInlineQuery: "InlineQuery",
WebhookInputCallbackQuery: "CallbackQuery",
WebhookInputChosenInlineResult: "ChosenInlineResult",
WebhookInputSubscribed: "Subscribed", // Viber
WebhookInputUnsubscribed: "Unsubscribed", // Viber
WebhookInputConversationStarted: "ConversationStarted", // Telegram
WebhookInputNewChatMembers: "NewChatMembers", // Telegram
WebhookInputSticker: "Sticker", // Telegram
WebhookInputLeftChatMembers: "LeftChatMembers", // Telegram
}

func GetWebhookInputTypeIdNameString(whInputType WebhookInputType) string {
name, ok := webhookInputTypeNames[whInputType]
if ok {
return fmt.Sprintf("%d:%s", whInputType, name)
}
return strconv.Itoa(int(whInputType))
}

// WebhookInput represent a single message
// '/entry/messaging' for Facebook Messenger
type WebhookInput interface {
GetSender() WebhookUser
GetRecipient() WebhookRecipient
GetTime() time.Time
InputType() WebhookInputType
BotChatID() (string, error)
Chat() WebhookChat
LogRequest() // TODO: should not be part of Input? If should - specify why
}

// WebhookActor represents sender
type WebhookActor interface {
Platform() string // TODO: Consider removing this?
GetID() any
IsBotUser() bool
GetFirstName() string
GetLastName() string
GetUserName() string
GetLanguage() string
}

// WebhookSender represents sender with avatar
type WebhookSender interface {
GetAvatar() string // Extension to support avatar (Viber)
WebhookActor
}

// WebhookUser represents sender with country
type WebhookUser interface {
WebhookSender

// GetCountry is an extension to support language & country (Viber)
GetCountry() string
}

// WebhookRecipient represents receiver
type WebhookRecipient interface {
WebhookActor
}

// WebhookMessage represents single message
type WebhookMessage interface {
IntID() int64
StringID() string
Chat() WebhookChat
//Sequence() int // 'seq' for Facebook, '???' for Telegram
}

// WebhookTextMessage represents single text message
type WebhookTextMessage interface {
WebhookMessage
Text() string
IsEdited() bool
}

// WebhookStickerMessage represents single sticker message
type WebhookStickerMessage interface {
WebhookMessage
// TODO: Define sticker message interface
}

// WebhookVoiceMessage represents single voice message
type WebhookVoiceMessage interface {
WebhookMessage
// TODO: Define voice message interface
}

// WebhookPhotoMessage represents single photo message
type WebhookPhotoMessage interface {
WebhookMessage
// TODO: Define photo message interface
}

// WebhookAudioMessage represents single audio message
type WebhookAudioMessage interface {
WebhookMessage
// TODO: Define audio message interface
}

// WebhookReferralMessage represents single referral message
// https://developers.facebook.com/docs/messenger-platform/webhook-reference/referral
type WebhookReferralMessage interface {
Type() string
Source() string
RefData() string
}

// WebhookContactMessage represents single contact message
type WebhookContactMessage interface {
GetPhoneNumber() string
GetFirstName() string
GetLastName() string
GetBotUserID() string
}

// WebhookNewChatMembersMessage represents single message about a new member of a botChat
type WebhookNewChatMembersMessage interface {
BotChatID() (string, error)
NewChatMembers() []WebhookActor
}

// WebhookLeftChatMembersMessage represents single message about a member leaving a botChat
type WebhookLeftChatMembersMessage interface {
BotChatID() (string, error)
LeftChatMembers() []WebhookActor
}

// WebhookChat represents botChat of a messenger
type WebhookChat interface {
GetID() string
GetType() string
IsGroupChat() bool
}

// WebhookPostback represents single postback message
type WebhookPostback interface {
PostbackMessage() interface{}
Payload() string
}

// WebhookSubscribed represents a subscription message
type WebhookSubscribed interface {
SubscribedMessage() interface{}
}

// WebhookUnsubscribed represents a message when user unsubscribe
type WebhookUnsubscribed interface {
UnsubscribedMessage() interface{}
}

// WebhookConversationStarted represents a single message about new conversation
type WebhookConversationStarted interface {
ConversationStartedMessage() interface{}
}

// WebhookInlineQuery represents a single inline message
type WebhookInlineQuery interface {
GetID() interface{}
GetInlineQueryID() string
GetFrom() WebhookSender
GetQuery() string
GetOffset() string
//GetLocation() - TODO: Not implemented yet
}

// WebhookDelivery represents a single delivery report message
type WebhookDelivery interface {
Payload() string
}

// WebhookChosenInlineResult represents a single report message on chosen inline result
type WebhookChosenInlineResult interface {
GetResultID() string
GetInlineMessageID() string // Telegram only?
GetFrom() WebhookSender
GetQuery() string
//GetLocation() - TODO: Not implemented yet
}

// WebhookCallbackQuery represents a single callback query message
type WebhookCallbackQuery interface {
GetID() string
GetInlineMessageID() string // Telegram only?
GetFrom() WebhookSender
GetMessage() WebhookMessage
GetData() string
Chat() WebhookChat
}

// WebhookAttachment represents attachment to a message
type WebhookAttachment interface {
Type() string // Enum(image, video, audio) for Facebook
PayloadUrl() string // 'payload.url' for Facebook
}
Loading

0 comments on commit 710ba6d

Please sign in to comment.