__( 'Telegram', 'advanced-form-integration' ), 'tasks' => array( 'send_message' => __( 'Send Message', 'advanced-form-integration' ), ) ); return $actions; } add_action( 'adfoin_action_fields', 'adfoin_telegram_action_fields' ); function adfoin_telegram_action_fields() { ?> 30 ) ); if ( is_wp_error( $response ) ) { wp_send_json_error( $response->get_error_message() ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $body['ok'] ) ) { wp_send_json_error( isset( $body['description'] ) ? $body['description'] : __( 'Unable to fetch updates. Check the Bot API Token.', 'advanced-form-integration' ) ); } $chats = array(); foreach ( (array) $body['result'] as $update ) { $message = isset( $update['message'] ) ? $update['message'] : ( isset( $update['channel_post'] ) ? $update['channel_post'] : null ); if ( empty( $message['chat']['id'] ) ) { continue; } $chat = $message['chat']; $label = ! empty( $chat['title'] ) ? $chat['title'] : trim( ( isset( $chat['first_name'] ) ? $chat['first_name'] : '' ) . ' ' . ( isset( $chat['last_name'] ) ? $chat['last_name'] : '' ) ); $chats[ $chat['id'] ] = array( 'id' => $chat['id'], 'title' => $label ? $label : $chat['id'], ); } wp_send_json_success( array_values( $chats ) ); } add_action( 'adfoin_telegram_job_queue', 'adfoin_telegram_job_queue', 10, 1 ); function adfoin_telegram_job_queue( $data ) { adfoin_telegram_send_data( $data['record'], $data['posted_data'] ); } function adfoin_telegram_send_data( $record, $posted_data ) { $record_data = json_decode( $record['data'], true ); if ( isset( $record_data['action_data']['cl'] ) && adfoin_check_conditional_logic( $record_data['action_data']['cl'], $posted_data ) ) { return; } $data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : array(); $task = isset( $record['task'] ) ? $record['task'] : ''; if ( 'send_message' !== $task ) { return; } $bot_api_key = isset( $data['bot_api_key'] ) ? $data['bot_api_key'] : ''; $chat_id = isset( $data['chat_id'] ) ? $data['chat_id'] : ''; $text = isset( $data['text'] ) ? adfoin_get_parsed_values( $data['text'], $posted_data ) : ''; if ( empty( $bot_api_key ) || empty( $chat_id ) || '' === $text || null === $text ) { return; } $url = 'https://api.telegram.org/bot' . $bot_api_key . '/sendMessage'; $args = array( 'timeout' => 30, 'method' => 'POST', 'headers' => array( 'Content-Type' => 'application/x-www-form-urlencoded' ), 'body' => array( 'chat_id' => $chat_id, 'text' => $text, ), ); $response = wp_remote_request( $url, $args ); adfoin_add_to_log( $response, $url, $args, $record ); }