MCPcopy Create free account
hub / github.com/bytebase/bytebase / sendDirectMessage

Function sendDirectMessage

backend/plugin/webhook/dingtalk/dingtalk.go:82–141  ·  view source on GitHub ↗

returns true if the message is sent successfully.

(webhookCtx webhook.Context)

Source from the content-addressed store, hash-verified

80
81// returns true if the message is sent successfully.
82func sendDirectMessage(webhookCtx webhook.Context) bool {
83 dingtalk := getDingTalkConfig(webhookCtx.IMSetting)
84 if dingtalk == nil {
85 return false
86 }
87 p := newProvider(dingtalk.ClientId, dingtalk.ClientSecret, dingtalk.RobotCode)
88 ctx := context.Background()
89
90 sent := map[string]bool{}
91 if err := common.Retry(ctx, func() error {
92 var errs error
93 var userPhones, userIDs []string
94 for _, u := range webhookCtx.MentionEndUsers {
95 phone, err := getDingTalkMobileFromUser(u)
96 if err != nil {
97 slog.Warn("failed to parse user phone", log.BBError(err), slog.String("user", u.Name))
98 continue
99 }
100 if phone == "" {
101 continue
102 }
103 if sent[phone] {
104 continue
105 }
106
107 userID, err := p.getIDByPhone(ctx, phone)
108 if err != nil {
109 err = errors.Wrapf(err, "failed to get user id by phone %v", phone)
110 multierr.AppendInto(&errs, err)
111 continue
112 }
113 if userID == "" {
114 // user not found
115 sent[phone] = true
116 continue
117 }
118 userIDs = append(userIDs, userID)
119 userPhones = append(userPhones, phone)
120 }
121 if len(userIDs) == 0 {
122 err := errors.Errorf("dingtalk dm: got 0 user id, errs: %v", errs)
123 return backoff.Permanent(err)
124 }
125
126 if err := p.sendMessage(ctx, userIDs, webhookCtx.TitleZh, getMarkdownText(webhookCtx)); err != nil {
127 err = errors.Wrapf(err, "failed to send message")
128 multierr.AppendInto(&errs, err)
129 } else {
130 for _, phone := range userPhones {
131 sent[phone] = true
132 }
133 }
134 return errs
135 }); err != nil {
136 slog.Warn("failed to send direct message to dingtalk users", log.BBError(err))
137 return false
138 }
139

Callers 1

PostMethod · 0.85

Calls 10

RetryFunction · 0.92
BBErrorFunction · 0.92
getDingTalkConfigFunction · 0.85
getMarkdownTextFunction · 0.85
getIDByPhoneMethod · 0.80
ErrorfMethod · 0.80
newProviderFunction · 0.70
StringMethod · 0.65
sendMessageMethod · 0.45

Tested by

no test coverage detected