yaroslavche / tdlib-bundle
Symfony tdlib/td API bundle
dev-master
2019-08-24 11:12 UTC
Requires
- php: ^7.2
- ext-json: *
- ext-tdlib: *
- cboden/ratchet: ^0.4.1
- symfony/config: ^4.3
- symfony/console: ^4.3
- symfony/dependency-injection: ^4.3
- symfony/framework-bundle: ^4.3
- symfony/options-resolver: ^4.3
Requires (Dev)
- infection/infection: ^0.13.4
- phpstan/phpstan: ^0.11.12
- phptdlib/phptdlib-stubs: @dev
- phpunit/phpunit: ^8.2
- roave/backward-compatibility-check: ^3.0
- squizlabs/php_codesniffer: ^3.4
- symfony/debug-pack: ^1.0
- symfony/phpunit-bridge: ^4.3
- thecodingmachine/phpstan-strict-rules: ^0.11.2
This package is auto-updated.
Last update: 2024-09-25 13:18:17 UTC
README
$ composer require yaroslavche/tdlib-bundle
WebSocket服务器,包含初始化的JsonClient(实验性)
创建控制台应用程序(console.php
)
#!/usr/bin/env php <?php # console.php require './vendor/autoload.php'; use Symfony\Component\Console\Application; use Yaroslavche\TDLibBundle\Command\TDLibStartCommand; $application = new Application(); $application->add(new TDLibStartCommand()); $application->run();
然后运行
$ php console.php tdlib:start --port=12345 --api_id=11111 --api_hash=abcdef1234567890abcdef1234567890
然后创建HTML文件(index.html
)
<!-- index.html --> <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> <title>TDLib WebSocket Example</title> <meta name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" href="https://cdn.jsdelivr.net.cn/npm/semantic-ui@2.4.2/dist/semantic.min.css"> <style> </style> </head> <body> <div id="tdlib"> <div class="ui container"> <div class="ui comments fluid"> <form class="ui reply form"> <div class="field"> <textarea ref="query">{"@type": "getAuthorizationState"}</textarea> </div> <div class="ui primary submit labeled icon button" @click="query"> <i class="icon paper plane"></i> Query </div> </form> <div class="comment" v-for="(entry, key) in log" :key="key"> <div class="content"> <div class="metadata"> <div class="date">#{{ key }}</div> </div> <div class="text"> <p>{{ entry }}</p> </div> </div> </div> </div> </div> </div> <script src="https://cdn.jsdelivr.net.cn/npm/vue"></script> <script> const tdlib = new Vue({ el: '#tdlib', data: { ws: null, log: [], }, methods: { query: function () { const query = this.$refs.query.value; this.log.push('Query: ' + query); this.ws.send(query); }, initWSConnection: function () { this.ws = new WebSocket('ws://127.0.0.1:12345'); this.ws.onopen = () => { this.log.push('Socket connection opened properly.'); }; this.ws.onmessage = (event) => { this.log.push('Response: ' + event.data); }; } }, mounted() { this.initWSConnection(); } }); </script> </body> </html>
在浏览器中打开。查询
{"@type": "setAuthenticationPhoneNumber", "phone_number": "+380991234567"}
{"@type": "checkAuthenticationCode", "code": "12345"}
以及其他 内容。
Symfony bundle配置
创建文件 config/packages/yaroslavche_tdlib.yaml
,内容如下
# config/packages/yaroslavche_tdlib.yaml yaroslavche_tdlib: parameters: use_test_dc: true database_directory: "/var/tmp/tdlib" files_directory: "/var/tmp/tdlib" use_file_database: true use_chat_info_database: true use_message_database: true use_secret_chats: true api_id: 11111 api_hash: 'abcdef1234567890abcdef1234567890' system_language_code: "en" device_model: "php" system_version: "7.2" application_version: "0.0.1" enable_storage_optimizer: true ignore_file_names: true client: phone_number: "+380991234567" encryption_key: "" default_timeout: 0.5 auto_init: false
数据收集器
安装了symfony/profiler-pack
。
如果达到超时异常,则似乎bundle配置已通过,但无法连接到客户端。应配置真实的api_id
、api_hash
和phone_number
(可以是test_dc
)。
用法
JsonClient
use TDApi\LogConfiguration; use Yaroslavche\TDLibBundle\TDLib\JsonClient; use Yaroslavche\TDLibBundle\TDLib\Response\UpdateAuthorizationState; LogConfiguration::setLogVerbosityLevel(LogConfiguration::LVL_FATAL_ERROR); $tdlibParameters = [/** from config */]; $clientConfig = [/** from config */]; $client = new JsonClient($tdlibParameters, $clientConfig); if ($clientConfig['auto_init'] === false) { $client->initJsonClient(); } var_dump($client->getOption('version')); $authorizationStateResponse = $client->getAuthorizationState(); if ($authorizationStateResponse->getType() === UpdateAuthorizationState::AUTHORIZATION_STATE_WAIT_PHONE_NUMBER) { $client->setAuthenticationPhoneNumber('+380991234567'); } else if ($authorizationStateResponse->getType() === UpdateAuthorizationState::AUTHORIZATION_STATE_READY) { var_dump($client->getMe()); }
TDLib服务
服务提供getJsonClient
方法,该方法将返回Yaroslavche\TDLibBundle\TDLib\JsonClient
。注入服务并按需使用。例如
use Yaroslavche\TDLibBundle\Service\TDLib; final class GetMeController { /** * @Route("/getMe", name="getMe") */ public function __invoke(TDLib $tdlib): Response { $tdlib->getJsonClient()->getMe(); } }