call( 'getMe' ); if ( ! $me['ok'] || empty( $me['result']['username'] ) || empty( $me['result']['is_bot'] ) ) { /* translators: %s: Telegram's error */ return new \WP_Error( 'orderchirp_token', sprintf( __( 'Telegram did not accept this token: %s', 'orderchirp' ), Telegram_Api::redact( $me['description'], $token ) ) ); } Settings::update( array( 'bot_token' => $token, 'bot_username' => (string) $me['result']['username'], ) ); return self::configure_delivery( $force_polling ); } /** * Point Telegram at the webhook, or fall back to polling. * * @param bool $force_polling Use polling even on HTTPS. * @return true|\WP_Error */ public static function configure_delivery( $force_polling = false ) { $api = Telegram_Api::from_settings(); $url = Webhook::url(); if ( ! $force_polling && 0 === strpos( $url, 'https://' ) ) { $set = $api->call( 'setWebhook', array( 'url' => $url, 'secret_token' => Settings::webhook_token( Settings::secret() ), 'allowed_updates' => array( 'message' ), 'max_connections' => 10, ) ); if ( $set['ok'] ) { Settings::update( array( 'delivery' => 'webhook' ) ); Poller::unschedule(); return true; } /* translators: %s: Telegram's error */ $error = new \WP_Error( 'orderchirp_webhook', sprintf( __( 'Telegram could not use this site as a webhook (%s). Orderchirp will check for messages once a minute instead.', 'orderchirp' ), Telegram_Api::redact( $set['description'], (string) Settings::get( 'bot_token' ) ) ) ); } $api->call( 'deleteWebhook' ); Settings::update( array( 'delivery' => 'polling' ) ); Poller::schedule(); return isset( $error ) ? $error : true; } /** * Disconnect: Telegram stops calling, the token is forgotten. Linked customers stay linked, * so connecting the same bot again resumes their updates. */ public static function disconnect() { if ( Settings::connected() ) { Telegram_Api::from_settings()->call( 'deleteWebhook', array(), 5 ); } Poller::unschedule(); Settings::update( array( 'bot_token' => '', 'bot_username' => '', ) ); } /** * What Telegram reports about delivery, for the settings page. * * @return array{ok:bool,delivery:string,pending:int,last_error:string,last_error_at:int} */ public static function status() { $delivery = (string) Settings::get( 'delivery' ); $info = Telegram_Api::from_settings()->call( 'getWebhookInfo', array(), 10 ); $result = is_array( $info['result'] ) ? $info['result'] : array(); return array( 'ok' => $info['ok'], 'delivery' => $delivery, 'pending' => isset( $result['pending_update_count'] ) ? (int) $result['pending_update_count'] : 0, 'last_error' => isset( $result['last_error_message'] ) ? (string) $result['last_error_message'] : ( $info['ok'] ? '' : $info['description'] ), 'last_error_at' => isset( $result['last_error_date'] ) ? (int) $result['last_error_date'] : 0, ); } /** * A t.me deep link for a payload. * * @param string $payload Payload. * @return string */ public static function deep_link( $payload ) { return 'https://t.me/' . rawurlencode( (string) Settings::get( 'bot_username' ) ) . '?start=' . rawurlencode( (string) $payload ); } }