php — Dodawanie przychodzącego webhooka Microsoft Teams do WordPress, problem z trasą odpoczynku?
Próbuję zaimplementować przychodzący webhook w mojej witrynie WordPress, aby wysłać wiadomość do MS Teams, gdy otrzymam przesłanie formularza. Wiadomość zawiera m.in ActionCard
z wejściem wielokrotnego wyboru aktualizującym status.
Mam wszystko, co działa, jeśli chodzi o wysyłanie wiadomości do Teams, w tym wprowadzanie wielokrotnego wyboru, ale kiedy dokonuję wyboru w wiadomości Teams i klikam przycisk, aby zaktualizować status, ma to wysłać odpowiedź z powrotem do mojej witryny, ale pojawia się komunikat o błędzie mówiący "Failed to send - There was a problem submitting your changes. Try again in a minute."
Zarejestrowałem niestandardową trasę odpoczynku, aby przechwycić odpowiedź, która działa dobrze, gdy używam JavaScript fetch()
, więc wiem, że docelowy adres URL działa; jednak nie mogę zmusić go do pracy z tą odpowiedzią Teams. Czy jest inna metoda, której powinienem używać lub czego mi brakuje?
Moja klasa:
class ERI_MICROSOFT_TEAMS {
/**
* The webhook for the MS Teams channel
*
* @var string
*/
public static $webhook;
/**
* The option name where we store the chat messages
*
* @var string
*/
public static $option_name;
/**
* Load when the site loads
*
* @return void
*/
public function init() {
// The webhook
self::$webhook = 'https://[hidden]';
// The option name
self::$option_name="microsoft_teams_webhook";
// Register custom API route for Teams
add_action( 'rest_api_init', [ $this, 'register_route' ] );
} // End init()
/**
* Process API $_POST request from Teams
*
* @param WP_REST_Request $request
* @return void
*/
public function process_request( WP_REST_Request $request ) {
// Get the request body
$message_data = $request->get_json_params();
// Attempt to do something with it, let's just try to update a site option for now
if ( update_option( self::$option_name, $message_data ) ) {
// If it worked, then send success response and status
$response = new WP_REST_Response( [ 'message' => 'Successful' ] );
$response->set_status( 200 );
return $response;
// Otherwise respond with an error
} else {
return new WP_Error( 'invalid_request', 'Something went wrong', [ 'status' => 403 ] );
}
} // End process_request()
/**
* Register a custom API route
*
*
* @return void
*/
public function register_route() {
register_rest_route( 'eri/v1', '/msteams', [
'methods' => 'POST',
'callback' => [ $this, 'process_request' ]
] );
} // End register_route()
/**
* Send a message to MS Teams channel
*
* @return void
*/
public static function send_msg( $form_id, $entry_id, $message, $name, $email, $phone="(none)", $user_id = '(no user found)' ) {
// Option prefix
$pf="msteams_msg_";
// Get the site name
if ( get_option( $pf.'site_name' ) && get_option( $pf.'site_name' ) != '' ) {
$site_name = get_option( $pf.'site_name' );
} else {
$site_name = get_bloginfo( 'name' );
}
// Get the site image
if ( get_option( $pf.'image' ) && get_option( $pf.'image' ) != '' ) {
$image = get_option( $pf.'image' );
} else {
$image=" // Our Logo
}
// Get the accent color
if ( get_option( $pf.'color' ) && get_option( $pf.'color' ) != '' ) {
$color = get_option( $pf.'color' );
} else {
$color="#FF0000"; // ERI Star
}
// Put the message card together
$data = [
'@type' => 'MessageCard',
'@context' => '
'summary' => 'Website Help Desk Support Request',
'themeColor' => $color,
'title' => 'New Support Request',
'sections' => [
[
'activityTitle' => $site_name,
'activitySubtitle' => date( 'Y-m-d' ),
'activityImage' => $image,
'text' => $message,
'facts' => [
[
'name' => 'Name: ',
'value' => $name
],
[
'name' => 'Email: ',
'value' => $email
],
],
]
],
'potentialAction' => [
[
'@type' => 'OpenUri',
'name' => 'Visit Site',
'targets' => [
[
'os' => 'default',
'uri' => home_url()
]
]
],
[
'@type' => 'ActionCard',
'name' => 'Update Status',
'inputs' => [
[
'@type' => 'MultichoiceInput',
'id' => 'status',
'title' => 'Status',
'choices' => [
[
'display' => 'Unresolved',
'value' => 'unresolved'
],
[
'display' => 'In Progress',
'value' => 'in_progress'
],
[
'display' => 'Resolved',
'value' => 'resolved'
]
]
]
],
'actions' => [ // WHERE I NEED HELP
[
'@type' => 'HttpPOST',
'name' => 'Update',
'target' => '',
'body' => '{{status.value}}'
]
]
],
]
];
// Encode
$json_data = json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
// Send it to discord
$ch = curl_init( self::$webhook );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [ 'Content-type: application/json' ] );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $json_data );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$response = curl_exec( $ch );
echo $response;
curl_close( $ch );
} // End send_msg()
}
Dokumentacja zawiera przykład JSON, a także próbowałem użyć Listonosza do wysłania, tak jak pokazuje ich przykład, ale pojawia się ten sam błąd, który sprawia, że wierzę, że jest to problem z moją trasą odpoczynku.