Skip to content

Commit

Permalink
fix: CreateGroup output type + handle better auto-accept group args
Browse files Browse the repository at this point in the history
Signed-off-by: ismael FALL <ismael.fall@epitech.eu>
  • Loading branch information
Doozers committed Dec 12, 2022
1 parent 270a5ab commit c6ddc5b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
6 changes: 3 additions & 3 deletions go/pkg/bertybot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ func (b *Bot) Start(ctx context.Context) error {
}
}

func (b *Bot) CreateGroup(ctx context.Context, name string) (string, error) {
func (b *Bot) CreateGroup(ctx context.Context, name string) (*messengertypes.ConversationCreate_Reply, error) {
conv, err := b.client.ConversationCreate(ctx, &messengertypes.ConversationCreate_Request{
DisplayName: name,
ContactsToInvite: nil,
})
if err != nil {
return "", fmt.Errorf("failed to create conversation: %w", err)
return nil, fmt.Errorf("failed to create conversation: %w", err)
}

return conv.GetPublicKey(), nil
return conv, nil
}
29 changes: 17 additions & 12 deletions go/pkg/bertybot/recipes.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,33 +130,38 @@ func DelayResponseRecipe(duration time.Duration) Recipe {
return recipe
}

type AutoAcceptIncomingGroupInviteOpts struct {
ConfirmationMessage string
}

// AutoAcceptIncomingGroupInviteRecipe makes the bot "click" on the "join" button automatically.
func AutoAcceptIncomingGroupInviteRecipe(confirmationMessage ...string) Recipe {
func AutoAcceptIncomingGroupInviteRecipe(opts *AutoAcceptIncomingGroupInviteOpts) Recipe {
if opts == nil {
opts = &AutoAcceptIncomingGroupInviteOpts{
ConfirmationMessage: "I'll join asap!",
}
}
recipe := map[HandlerType][]Handler{}
recipe[IncomingGroupInvitationHandler] = []Handler{
func(ctx Context) {
var err error
if confirmationMessage != nil {
err = ctx.ReplyString("I'll join asap!")
} else {
err = ctx.ReplyString(confirmationMessage[0])
}
if err != nil {
if opts.ConfirmationMessage != "" {
err := ctx.ReplyString(opts.ConfirmationMessage)
ctx.Logger.Error("reply failed", zap.Error(err))
// continue
}

payload, err := ctx.Interaction.UnmarshalPayload()
if err != nil {
ctx.Logger.Error("parse payload", zap.Error(err))
return
}
invLink := payload.(*messengertypes.AppMessage_GroupInvitation).Link

req := messengertypes.ConversationJoin_Request{Link: invLink}
inviteLink := payload.(*messengertypes.AppMessage_GroupInvitation).Link
ctx.Logger.Info("auto-joining incoming group", zap.String("link", inviteLink))
req := messengertypes.ConversationJoin_Request{Link: inviteLink}
_, err = ctx.Client.ConversationJoin(ctx.Context, &req)
if err != nil {
ctx.Logger.Error("failed to join group", zap.Error(err))
}
ctx.Logger.Info("auto-joining incoming group", zap.String("link", invLink))
},
}
return recipe
Expand Down

0 comments on commit c6ddc5b

Please sign in to comment.