' . esc_html__( 'In the channel: ••• → Workflows → "Post to a channel when a webhook request is received", finish the wizard and copy the URL.', 'fieldwren' ) . '
'; } public function credential_fields() { return array( 'webhook_url' => array( 'label' => __( 'Default webhook URL', 'fieldwren' ), 'type' => 'password', 'placeholder' => 'https://…logic.azure.com/…' ), ); } public function can_verify() { return true; } public function verify_label() { return __( 'Send a test message', 'fieldwren' ); } public function verify( array $creds ) { if ( empty( $creds['webhook_url'] ) ) { return new \WP_Error( 'fwren_missing', __( 'Add a webhook URL first.', 'fieldwren' ) ); } $res = $this->http( 'POST', $creds['webhook_url'], array( 'body' => self::card( __( 'Fieldwren is connected.', 'fieldwren' ), array(), '', '' ) ) ); return is_wp_error( $res ) ? $res : true; } public function feed_fields( $form_id ) { return array_merge( array( array( 'key' => 'webhook', 'type' => 'secret', 'url' => true, 'label' => __( 'Webhook URL', 'fieldwren' ), 'desc' => __( 'Leave empty to use the default webhook.', 'fieldwren' ) ) ), $this->message_fields() ); } /** @return array Message with one Adaptive Card. */ private static function card( $title, array $facts, $text, $url ) { $body = array( array( 'type' => 'TextBlock', 'text' => $title, 'weight' => 'Bolder', 'size' => 'Medium', 'wrap' => true ) ); if ( $text ) { $body[] = array( 'type' => 'TextBlock', 'text' => $text, 'wrap' => true ); } if ( $facts ) { $body[] = array( 'type' => 'FactSet', 'facts' => $facts ); } $card = array( '$schema' => 'http://adaptivecards.io/schemas/adaptive-card.json', 'type' => 'AdaptiveCard', 'version' => '1.4', 'body' => $body, 'msteams' => array( 'width' => 'Full' ), ); if ( $url ) { $card['actions'] = array( array( 'type' => 'Action.OpenUrl', 'title' => __( 'View entry', 'fieldwren' ), 'url' => $url ) ); } return array( 'type' => 'message', 'attachments' => array( array( 'contentType' => 'application/vnd.microsoft.card.adaptive', 'contentUrl' => null, 'content' => $card ) ), ); } public function process( array $feed, array $entry, array $values ) { $s = $feed['settings']; $url = trim( (string) ( $s['webhook'] ?? '' ) ); $url = '' !== $url ? $url : $this->cred( 'webhook_url' ); if ( ! $url ) { return new \WP_Error( 'fwren_missing', __( 'No Teams webhook URL.', 'fieldwren' ) ); } $this->secrets[] = $url; $facts = array(); foreach ( $this->message_rows() as $r ) { $facts[] = array( 'title' => self::cut( $r[0], 200 ), 'value' => self::cut( $r[1], 2000 ) ); } $meta = $this->entry_meta(); $title = $this->sval( 'title' ); $res = $this->http( 'POST', $url, array( 'body' => self::card( $title ? $title : $this->default_title(), $facts, $this->sval( 'message' ), ! empty( $s['link'] ) ? $meta['entry_url'] : '' ) ) ); return is_wp_error( $res ) ? $res : __( 'Posted to Teams.', 'fieldwren' ); } }