Skip to content

Instantly share code, notes, and snippets.

@mihdan
Last active November 5, 2023 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mihdan/ae0b518e248dc6b45c48d60a76790f55 to your computer and use it in GitHub Desktop.
Save mihdan/ae0b518e248dc6b45c48d60a76790f55 to your computer and use it in GitHub Desktop.
Отправляет все формы Contacy Form 7 с сайта в приватный канал в Telegram
<?php
/**
* CF7 to Telegram
*/
namespace Mihdan\Kadence_Child;
/**
* Class ContactFormToTelegram
*
* @package Mihdan\Kadence_Child
*/
class ContactFormToTelegram {
private $args;
public function __construct() {
$this->rgs = [
'bot_token' => TELEGRAM_BOT_TOKEN,
'receivers' => [
TELEGRAM_RECEIVERS,
],
];
}
public function setup_hooks() {
add_filter( 'wpcf7_mail_components', [ $this, 'to_tlg_action' ], 10, 3 );
}
public function to_tlg_send_message( $message, $receiver ) {
$params['text'] = wp_strip_all_tags( $message );
$params['chat_id'] = (int) $receiver;
$api_url = sprintf(
'https://api.telegram.org/bot%s/sendMessage?%s',
$this->args['bot_token'],
http_build_query( $params )
);
$response = wp_remote_get( $api_url );
if ( is_wp_error( $response ) ) {
error_log( 'Error in cf7-to-tlg: ' . $response );
} else {
$response_body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! $response_body['ok'] ) {
error_log( 'Error in cf7-to-tlg: ' . json_encode( $response_body ) );
}
}
}
public function to_tlg_action( $components, $form, $instance ) {
foreach ( $this->args['receivers'] as $receiver ) {
$this->to_tlg_send_message( $components['body'], $receiver );
}
return $components;
}
}
( new ContactFormToTelegram() )->setup_hooks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment