';
}
public function description() {
return __( 'Send entries to a Telegram chat, group or channel through your bot.', 'fieldwren' );
}
public function help() {
return '- ' . esc_html__( 'Talk to @BotFather, send /newbot and paste the token here.', 'fieldwren' ) . '
- ' . esc_html__( 'Add the bot to the group or channel (as admin for channels) and send it a message.', 'fieldwren' ) . '
- ' . esc_html__( 'In the feed, pick the chat from "Recent chats" or type its id (e.g. -1001234567890 or @channelname).', 'fieldwren' ) . '
';
}
public function credential_fields() {
return array(
'bot_token' => array( 'label' => __( 'Bot token', 'fieldwren' ), 'type' => 'password', 'required' => true, 'placeholder' => '123456:ABC-DEF…' ),
);
}
/** @return string API URL of a method. */
private function url( $method, $token = null ) {
return 'https://api.telegram.org/bot' . ( null === $token ? $this->cred( 'bot_token' ) : $token ) . '/' . $method;
}
protected function error_message( $body, $code ) {
return is_array( $body ) && isset( $body['description'] ) ? (string) $body['description'] : parent::error_message( $body, $code );
}
public function verify( array $creds ) {
$this->secrets[] = (string) $creds['bot_token'];
$b = self::body( $this->http( 'GET', $this->url( 'getMe', (string) $creds['bot_token'] ) ) );
return is_wp_error( $b ) ? $b : '@' . ( $b['result']['username'] ?? '' );
}
public function options( $source, array $settings ) {
if ( 'chats' !== $source ) {
return parent::options( $source, $settings );
}
$b = self::body( $this->http( 'POST', $this->url( 'getUpdates' ), array( 'body' => array( 'limit' => 100 ) ) ) );
if ( is_wp_error( $b ) ) {
return $b;
}
$out = array();
foreach ( (array) ( $b['result'] ?? array() ) as $u ) {
foreach ( array( 'message', 'edited_message', 'channel_post', 'my_chat_member' ) as $k ) {
$c = $u[ $k ]['chat'] ?? null;
if ( $c ) {
$name = $c['title'] ?? trim( ( $c['first_name'] ?? '' ) . ' ' . ( $c['last_name'] ?? '' ) );
$out[ (string) $c['id'] ] = $name . ' (' . $c['type'] . ', ' . $c['id'] . ')';
}
}
}
return $out;
}
public function feed_fields( $form_id ) {
return array_merge(
array(
array( 'key' => 'chat', 'type' => 'select', 'source' => 'chats', 'label' => __( 'Recent chats', 'fieldwren' ), 'empty' => __( '— Type the chat id below —', 'fieldwren' ), 'desc' => __( 'Chats that messaged the bot in the last 24 hours. Not listed? Type the id below.', 'fieldwren' ) ),
array( 'key' => 'chat_id', 'type' => 'text', 'label' => __( 'Chat id', 'fieldwren' ), 'placeholder' => '-1001234567890 / @channel', 'smartcodes' => false, 'desc' => __( 'Used when filled; otherwise the chat picked above.', 'fieldwren' ) ),
array( 'key' => 'thread_id', 'type' => 'text', 'label' => __( 'Topic id', 'fieldwren' ), 'smartcodes' => false, 'desc' => __( 'Optional: forum topic of a supergroup.', 'fieldwren' ) ),
),
$this->message_fields(),
array( array( 'key' => 'silent', 'type' => 'toggle', 'label' => __( 'Silent', 'fieldwren' ), 'text' => __( 'Send without a notification sound', 'fieldwren' ) ) )
);
}
public function process( array $feed, array $entry, array $values ) {
$s = $feed['settings'];
$chat = trim( (string) ( $s['chat_id'] ?? '' ) );
$chat = '' !== $chat ? $chat : trim( (string) ( $s['chat'] ?? '' ) );
if ( '' === $chat ) {
return new \WP_Error( 'fwren_missing', __( 'No Telegram chat id.', 'fieldwren' ) );
}
$e = static function ( $t ) {
return htmlspecialchars( (string) $t, ENT_NOQUOTES, 'UTF-8' );
};
$title = $this->sval( 'title' );
$lines = array( '' . $e( $title ? $title : $this->default_title() ) . '' );
$text = $this->sval( 'message' );
if ( $text ) {
$lines[] = $e( $text );
}
$data = array();
foreach ( $this->message_rows() as $r ) {
$data[] = '' . $e( $r[0] ) . ': ' . $e( $r[1] );
}
if ( $data ) {
$lines[] = implode( "\n", $data );
}
$meta = $this->entry_meta();
if ( ! empty( $s['link'] ) && $meta['entry_url'] ) {
$lines[] = '' . $e( __( 'View entry', 'fieldwren' ) ) . ( $meta['entry_id'] ? ' #' . $meta['entry_id'] : '' ) . '';
}
$msg = implode( "\n\n", $lines );
if ( strlen( $msg ) > 4000 ) {
$msg = $e( self::cut( html_entity_decode( wp_strip_all_tags( $msg ), ENT_QUOTES, 'UTF-8' ), 3900 ) );
}
$body = array(
'chat_id' => $chat,
'text' => $msg,
'parse_mode' => 'HTML',
'disable_web_page_preview' => true,
'disable_notification' => ! empty( $s['silent'] ),
);
if ( ! empty( $s['thread_id'] ) && is_numeric( $s['thread_id'] ) ) {
$body['message_thread_id'] = (int) $s['thread_id'];
}
$b = self::body( $this->http( 'POST', $this->url( 'sendMessage' ), array( 'body' => $body ) ) );
return is_wp_error( $b ) ? $b : __( 'Sent to Telegram.', 'fieldwren' );
}
}