Skip to content

Commit

Permalink
Add invoke, add msg type
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Nov 14, 2023
1 parent 7b5cb8f commit 5b396bf
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 53 deletions.
134 changes: 100 additions & 34 deletions pkg/api/cloudteams/cloud_teams.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/bot/interactive/plugin_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var pluginHelpProvider = map[string]pluginHelpProviderFn{
}
},
"botkube/kubectl": func(platform config.CommPlatformIntegration, btnBuilder *api.ButtonBuilder) api.Section {
if platform.IsInteractive() {
// TODO(https://github.com/kubeshop/botkube-cloud/issues/645): add support for kubectl builder
if platform.IsInteractive() && platform != config.CloudTeamsCommPlatformIntegration {
return api.Section{
Base: api.Base{
Header: "Run kubectl commands",
Expand Down
46 changes: 31 additions & 15 deletions pkg/bot/teams_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/avast/retry-go/v4"
"github.com/infracloudio/msbotbuilder-go/core/activity"
"github.com/infracloudio/msbotbuilder-go/schema"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"

Expand All @@ -26,8 +27,6 @@ import (
"github.com/kubeshop/botkube/pkg/sliceutil"
)

const channelIDKeyName = "teamsChannelId"

var _ Bot = &CloudTeams{}

// CloudTeams listens for user's messages, execute commands and sends back the response.
Expand Down Expand Up @@ -92,7 +91,6 @@ func NewCloudTeams(
if err != nil {
return nil, err
}

return &CloudTeams{
log: log,
executorFactory: executorFactory,
Expand Down Expand Up @@ -254,9 +252,12 @@ func (b *CloudTeams) handleStreamMessage(ctx context.Context, data *pb.CloudActi
return nil, fmt.Errorf("while unmarshaling activity event: %w", err)
}
switch act.Type {
case schema.Message:
case schema.Message, schema.Invoke:
b.log.WithField("message", formatx.StructDumper().Sdump(act)).Debug("Processing Cloud message...")
channel, exists := b.getChannelForActivity(act)
channel, exists, err := b.getChannelForActivity(act)
if err != nil {
b.log.WithError(err).Error("cannot extract message channel id, processing with empty...")
}

msg := b.processMessage(ctx, act, channel, exists)
if msg.IsEmpty() {
Expand All @@ -273,6 +274,7 @@ func (b *CloudTeams) handleStreamMessage(ctx context.Context, data *pb.CloudActi
conversationRef := activity.GetCoversationReference(act)
return &pb.AgentActivity{
Message: &pb.Message{
MessageType: pb.MessageType_MESSAGE_EXECUTOR,
TeamId: channel.teamID,
ConversationId: conversationRef.Conversation.ID,
ActivityId: conversationRef.ActivityID, // activity ID allows us to send it as a thread message
Expand Down Expand Up @@ -301,7 +303,8 @@ func (b *CloudTeams) processMessage(ctx context.Context, act schema.Activity, ch
},
Message: trimmedMsg,
User: execute.UserInput{
//Mention: "", // TODO(https://github.com/kubeshop/botkube-cloud/issues/677): set when adding interactivity support.
// TODO: we need to add support for mentions on cloud side.
//Mention: "",
DisplayName: act.From.Name,
},
})
Expand All @@ -322,6 +325,7 @@ func (b *CloudTeams) sendAgentActivity(ctx context.Context, msg interactive.Core

act := &pb.AgentActivity{
Message: &pb.Message{
MessageType: pb.MessageType_MESSAGE_SOURCE,
TeamId: channel.teamID,
ActivityId: "", // empty so it will be sent on root instead of sending as a thread message
ConversationId: channel.ID,
Expand All @@ -338,19 +342,31 @@ func (b *CloudTeams) sendAgentActivity(ctx context.Context, msg interactive.Core
return errs.ErrorOrNil()
}

func (b *CloudTeams) getChannelForActivity(act schema.Activity) (teamsCloudChannelConfigByID, bool) {
rawChannelID, exists := act.ChannelData[channelIDKeyName]
if !exists {
return teamsCloudChannelConfigByID{}, false
type channelData struct {
Channel struct {
ID string `mapstructure:"id"`
} `mapstructure:"channel"`
TeamsChannelID string `mapstructure:"teamsChannelId"`
}

func (b *CloudTeams) getChannelForActivity(act schema.Activity) (teamsCloudChannelConfigByID, bool, error) {
var data channelData
err := mapstructure.Decode(act.ChannelData, &data)
if err != nil {
return teamsCloudChannelConfigByID{}, false, fmt.Errorf("while decoding data: %w", err)
}

channelID, ok := rawChannelID.(string)
if !ok {
return teamsCloudChannelConfigByID{}, false
if data.Channel.ID == "" && data.TeamsChannelID == "" {
return teamsCloudChannelConfigByID{}, false, fmt.Errorf("cannot find channel id in: %s", formatx.StructDumper().Sdump(act.ChannelData))
}

channel, exists := b.getChannels()[channelID]
return channel, exists
id := data.TeamsChannelID
if id == "" {
id = data.Channel.ID
}

channel, exists := b.getChannels()[id]
return channel, exists, nil
}

func (b *CloudTeams) getChannelsToNotify(sourceBindings []string) []teamsCloudChannelConfigByID {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const (
)

func (c CommPlatformIntegration) IsInteractive() bool {
return c == SocketSlackCommPlatformIntegration || c == CloudSlackCommPlatformIntegration
return c == SocketSlackCommPlatformIntegration || c == CloudSlackCommPlatformIntegration || c == CloudTeamsCommPlatformIntegration
}

// String returns string platform name.
Expand Down
11 changes: 10 additions & 1 deletion pkg/execute/plugin_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (e *PluginExecutor) Execute(ctx context.Context, bindings []string, slackSt
Command: cmdCtx.CleanCmd,
Configs: configs,
Context: executor.ExecuteInputContext{
IsInteractivitySupported: cmdCtx.Platform.IsInteractive(),
IsInteractivitySupported: e.doesPlatformInteractivity(cmdCtx),
SlackState: slackState,
KubeConfig: kubeconfig,
Message: executor.Message{
Expand Down Expand Up @@ -146,6 +146,15 @@ func (e *PluginExecutor) Execute(ctx context.Context, bindings []string, slackSt
return out, nil
}

func (e *PluginExecutor) doesPlatformInteractivity(cmdCtx CommandContext) bool {
// TODO(https://github.com/kubeshop/botkube-cloud/issues/645): add support for kubectl builder
if strings.EqualFold(cmdCtx.CleanCmd, "kubectl") && cmdCtx.Platform == config.CloudTeamsCommPlatformIntegration {
// event though the cloud Teams support some interactivity, we do not support the command builder yet.
return false
}
return cmdCtx.Platform.IsInteractive()
}

func allMessagesMarkedAsSkip(msgs []api.Message) bool {
for _, item := range msgs {
if item.Type != api.SkipMessage {
Expand Down
8 changes: 7 additions & 1 deletion proto/cloud_teams.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ message AgentActivity {
Message message = 2;
}

enum MessageType {
MESSAGE_EXECUTOR = 0;
MESSAGE_SOURCE = 1;
}

message Message {
string teamId = 1;
string activityId = 2;
string conversationId = 3;
bytes data = 4;
MessageType messageType = 4;
bytes data = 5;
}

message CloudActivity {
Expand Down

0 comments on commit 5b396bf

Please sign in to comment.