fix(mcp-server): set Authorization header on Send{Message,File,AudioVoiceMessage}

The three Send* helpers in helpers/whatsapp.go construct their HTTP
requests directly instead of going through callAPI, and were missing the
Bearer token that the bridge's JWT middleware requires. Calls reached the
bridge as 401 Unauthorized.

In practice sendMessageHandler in mcp_tool.go bypasses SendMessage and
calls callAPI directly, so the text-send path happens to work; but the
file and audio-message MCP handlers route through SendFile and
SendAudioVoiceMessage respectively, so send_file and send_audio_message
were broken end-to-end.

Same shape as the recent DownloadMedia fix: fetch a JWT via
GetOrRefreshJwtToken and add Authorization: Bearer <token> on the
outgoing request.
This commit is contained in:
M. L. Giannotta
2026-05-10 17:20:43 +08:00
parent c9523748f9
commit 3c9886fa10
+18
View File
@@ -67,6 +67,11 @@ func SendMessage(recipient, message string) (bool, string) {
return false, "Recipient must be provided"
}
token, err := GetOrRefreshJwtToken()
if err != nil {
return false, "Authentication failed: " + err.Error()
}
payload := map[string]string{
"recipient": recipient,
"message": message,
@@ -75,6 +80,7 @@ func SendMessage(recipient, message string) (bool, string) {
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", apiBaseURL+"/send", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
client := &http.Client{}
resp, err := client.Do(req)
@@ -113,6 +119,11 @@ func SendFile(recipient, mediaPath string) (bool, string) {
return false, "Media file not found: " + mediaPath
}
token, err := GetOrRefreshJwtToken()
if err != nil {
return false, "Authentication failed: " + err.Error()
}
payload := map[string]string{
"recipient": recipient,
"media_path": mediaPath,
@@ -121,6 +132,7 @@ func SendFile(recipient, mediaPath string) (bool, string) {
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", apiBaseURL+"/send", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
client := &http.Client{}
resp, err := client.Do(req)
@@ -174,6 +186,11 @@ func SendAudioVoiceMessage(recipient, mediaPath string) (bool, string) {
}(finalPath)
}
token, err := GetOrRefreshJwtToken()
if err != nil {
return false, "Authentication failed: " + err.Error()
}
payload := map[string]string{
"recipient": recipient,
"media_path": finalPath,
@@ -182,6 +199,7 @@ func SendAudioVoiceMessage(recipient, mediaPath string) (bool, string) {
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", apiBaseURL+"/send", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
client := &http.Client{}
resp, err := client.Do(req)