danog/telegram-entities
Composer 安装命令:
composer require danog/telegram-entities
包简介
A library to work with Telegram UTF-16 styled text entities.
README 文档
README
A library to work with Telegram UTF-16 styled text entities, created by Daniil Gentili (https://daniil.it).
This library can be used to modify entities returned by the Telegram Bot API, convert entities to HTML or even locally generate them using a custom MarkdownV2 and HTML parser inside of the library.
This library was initially created for MadelineProto, an async PHP client API for the telegram MTProto protocol.
Installation
composer require danog/telegram-entities
Usage
<?php declare(strict_types=1); use Amp\Http\Client\HttpClientBuilder; use Amp\Http\Client\Request; use danog\TelegramEntities\Entities; use danog\TelegramEntities\EntityTools; require __DIR__.'/../vendor/autoload.php'; $token = getenv('TOKEN'); if (!$token) { throw new AssertionError("A TOKEN environment variable must be specified!"); } $dest = getenv('DEST'); if (!$dest) { throw new AssertionError("A DEST environment variable must be specified!"); } $client = HttpClientBuilder::buildDefault(); $sm = function (string $message, string $parse_mode = '', array $entities = []) use ($token, $dest, $client): array { $res = $client->request(new Request("https://api.telegram.org/bot$token/sendMessage?".http_build_query([ 'text' => $message, 'parse_mode' => $parse_mode, 'entities' => json_encode($entities), 'chat_id' => $dest ]))); return json_decode($res->getBody()->buffer(), true)['result']; }; $result = $sm("*This is a ❤️ test*", parse_mode: "MarkdownV2"); // Convert a message+entities back to HTML $entities = new Entities($result['text'], $result['entities']); var_dump($entities->toHTML()); // <b>This is a ❤️ test</b> // Modify $entities as needed $entities->message = "A message with ❤️ emojis"; // EntityTools::mb* methods compute the length in UTF-16 code units, as required by the bot API. $entities->entities[0]['length'] = EntityTools::mbStrlen($entities->message); // then resend: $sm($entities->message, entities: $entities->entities); // Convert HTML to an array of entities locally $entities = Entities::fromHtml("<b>This is <i>a ❤️ nested</i> test</b>"); $sm($entities->message, entities: $entities->entities); // Convert markdown to an array of entities locally $entities = Entities::fromMarkdown("*This is _a ❤️ nested_ test*"); $sm($entities->message, entities: $entities->entities); // Escape text using utility methods $generic = EntityTools::markdownEscape("Automatically escaped to prevent *markdown injection*!"); $link = EntityTools::markdownUrlEscape("https://google.com"); $code = EntityTools::markdownCodeEscape("test with autoescaped ` test"); $codeBlock = EntityTools::markdownCodeblockEscape("<?php echo 'test with autoescaped ``` test';"); $entities = Entities::fromMarkdown("This is _a ❤️ [nested]($link)_ `$code` \```php $codeBlock \``` $generic "); $sm($entities->message, entities: $entities->entities); // Escape text for the HTML parser! $generic = EntityTools::htmlEscape("Automatically escaped to prevent <b>HTML injection</b>!"); $entities = Entities::fromHtml($generic); $sm($entities->message, entities: $entities->entities); // See https://github.com/danog/telegram-entities for the full list of available methods!
Many more methods are available, see the API documentation for the full list!
API Documentation
Click here » to view the API documentation.
danog/telegram-entities 适用场景与选型建议
danog/telegram-entities 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 250.45k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2024 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 danog/telegram-entities 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 danog/telegram-entities 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 250.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 19
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2024-04-15