使用 telegram 的 TL 模式语言处理“标志”类型

2024-04-10

我编写了一个 tl 解析器,因此现在可以使用最新的层(53)。但我不确定如何处理“标志”类型。它们仅在 tl 文档中提到,但在页面底部没有定义(据我所知):link https://core.telegram.org/mtproto/TL-formal.

例如,当一个方法返回“消息”类型时,它应该如下所示:message#c09be45f flags:# out:flags.1?true mentioned:flags.4?true media_unread:flags.5?true silent:flags.13?true post:flags.14?true id:int from_id:flags.8?int to_id:Peer fwd_from:flags.2?MessageFwdHeader via_bot_id:flags.11?int reply_to_msg_id:flags.3?int date:int message:string media:flags.9?MessageMedia reply_markup:flags.6?ReplyMarkup entities:flags.7?Vector<MessageEntity> views:flags.10?int edit_date:flags.15?int = Message;

如果我理解正确的话,每个标志都是在某个变量中设置的,对吗?

我的解析器像这样分解“消息”类型:

     id: -1063525281
     params:
             name: flags
             type:
                     name: out
                     bit: 1
                     type: true

                     name: mentioned
                     bit: 4
                     type: true

                     name: media_unread
                     bit: 5
                     type: true

                     name: silent
                     bit: 13
                     type: true

                     name: post
                     bit: 14
                     type: true

                     name: from_id
                     bit: 8
                     type: int

                     name: fwd_from
                     bit: 2
                     type: MessageFwdHeader

                     name: via_bot_id
                     bit: 11
                     type: int

                     name: reply_to_msg_id
                     bit: 3
                     type: int

                     name: media
                     bit: 9
                     type: MessageMedia

                     name: reply_markup
                     bit: 6
                     type: ReplyMarkup

                     name: entities
                     bit: 7
                     type: Vector<MessageEntity>

                     name: views
                     bit: 10
                     type: int

                     name: edit_date
                     bit: 15
                     type: int

             name: id
             type: int

             name: to_id
             type: Peer

             name: date
             type: int

             name: message
             type: string

     predicate: message
     type: Message

但是,如果标志是某个变量中的位,那么是哪个变量呢?

相关:tl 是基于正式的标准化语言规范还是专门为 telegram 创建的?我问这个问题是因为如果它是正式语言(如 yaml)的子集,那么最好使用已知的 tl 解析器,而不是重新发明轮子。


但是,如果标志是某个变量中的位,那么是哪个变量呢?

Message#c09be45f flags:# out:flags.1?true mentioned:flags.4?true media_unread:flags.5?true silent:flags.13?true post:flags.14?true id:int from_id:flags.8?int to_id:Peer fwd_from:flags.2?MessageFwdHeader via_bot_id:flags.11?int reply_to_msg_id:flags.3?int date:int message:string media:flags.9?MessageMedia reply_markup:flags.6?ReplyMarkup entities:flags.7?Vector<MessageEntity> views:flags.10?int edit_date:flags.15?int = Message;

Example: flags:# out:flags.1?true

解码标志: BinaryAND(flags, 2^ix) === 2^ix --> 这将帮助您确定是否包含某个字段

flags= 如果该值flags字段,这通常是带有标志的对象的第一个字段

ix == 标志索引,这是一个指示标志位置的数字,例如out:flags.1?true这里out是flag位置1的字段,类型为true

对于上面的例子out将有一个值为true if BAND(flags,2^N) == 2^N否则,out字段被忽略

代码 - 消息编码 (Elixir)

def encode(%Message{} = x), do: <<95, 228, 155, 192, encode(:Int, x.flags)::binary, enc_f(:True, x.out, x.flags, 2)::binary, enc_f(:True, x.mentioned, x.flags, 16)::binary, enc_f(:True, x.media_unread, x.flags, 32)::binary, enc_f(:True, x.silent, x.flags, 8192)::binary, enc_f(:True, x.post, x.flags, 16384)::binary, encode(:Int, x.id)::binary, enc_f(:Int, x.from_id, x.flags, 256)::binary, encode(x.to_id)::binary, enc_f(x.fwd_from, x.flags, 4)::binary, enc_f(:Int, x.via_bot_id, x.flags, 2048)::binary, enc_f(:Int, x.reply_to_msg_id, x.flags, 8)::binary, encode(:Int, x.date)::binary, encode(:String, x.message)::binary, enc_f(x.media, x.flags, 512)::binary, enc_f(x.reply_markup, x.flags, 64)::binary, enc_vf(x.entities, x.flags, 128)::binary, enc_f(:Int, x.views, x.flags, 1024)::binary, enc_f(:Int, x.edit_date, x.flags, 32768)::binary>>

代码 - 消息解码 (Elixir)

  def decode(<<95, 228, 155, 192, bin::binary>>) do
    {flags, bin} = decode(:Int, bin)
    {out, bin} = decode(:True, bin, flags, 2) # 1
    {mentioned, bin} = decode(:True, bin, flags, 16) # 4
    {media_unread, bin} = decode(:True, bin, flags, 32) # 5
    {silent, bin} = decode(:True, bin, flags, 8192) # 13
    {post, bin} = decode(:True, bin, flags, 16384) # 14
    {id, bin} = decode(:Int, bin)
    {from_id, bin} = decode(:Int, bin, flags, 256) # 8
    {to_id, bin} = decode(bin)
    {fwd_from, bin} = decode(bin, flags, 4) # 2
    {via_bot_id, bin} = decode(:Int, bin, flags, 2048) # 11
    {reply_to_msg_id, bin} = decode(:Int, bin, flags, 8) # 3
    {date, bin} = decode(:Int, bin)
    {message, bin} = decode(:String, bin)
    {media, bin} = decode(bin, flags, 512) # 9
    {reply_markup, bin} = decode(bin, flags, 64) # 6
    {entities, bin} = decode([:MessageEntity], bin, flags, 128) # 7
    {views, bin} = decode(:Int, bin, flags, 1024) # 10
    {edit_date, bin} = decode(:Int, bin, flags, 32768) # 15
    {%Message{flags: flags, out: out, mentioned: mentioned, media_unread: media_unread, silent: silent, post: post, id: id, from_id: from_id, to_id: to_id, fwd_from: fwd_from, via_bot_id: via_bot_id, reply_to_msg_id: reply_to_msg_id, date: date, message: message, media: media, reply_markup: reply_markup, entities: entities, views: views, edit_date: edit_date}, bin}
  end

#5 == 2^5 == 32
#4 == 2^4 == 16

所以基本上N == 2^N, where N == ix

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 telegram 的 TL 模式语言处理“标志”类型 的相关文章

随机推荐

  • 防止 ejs for 循环中出现空行

    我有以下简单的 ejs 模板 这会产生以下结果 0 1 2 3 4 5 6 7 8 9 如何防止 ejs 创建这样的空行 如果可能的话 我不想修改结果字符串 而是告诉 ejs 不要渲染这些空行 我怎样才能做到这一点 修剪模式 newline
  • 张量流中的正则化损失是什么?

    当使用 Tensorflows 对象检测 API 训练对象检测 DNN 时 它的可视化平台 Tensorboard 会绘制一个名为regularization loss 1 这是什么 我知道什么是正则化 使网络善于通过各种方法 例如 dro
  • GreenDAO支持表之间的多种关系

    我一直在尝试使用 GreenDAO 创建数据库模型 当我尝试在不同表之间创建多个关系时 问题就开始了 基本上 我有一个Message桌子 一个Conversation表和一个User table 用户有一个消息列表 并且该消息有一个父对话
  • printf 命令导致段错误? [复制]

    这个问题在这里已经有答案了 当我尝试初始化一个大型的二维字符数组时 它工作得很好 但是当我添加一个简单的打印命令时 它给了我一个分段错误 关于为什么会发生这种情况有什么想法吗 include
  • 如何将 Zlib 与 Cmake 链接

    我试图将我的文件与 zlib 库链接 但仍然得到 对 deflateInit 的未定义引用 我目前正在使用CLion 已从主页下载了zLib文件并将其添加到项目中 这就是我的 CmakeLists txt 的样子 cmake minimum
  • 查找通过代理连接的客户端的IP地址

    有没有办法收集通过代理服务器连接到您网站的客户端的 IP 地址 整个设置是一个内部 LAN 通过系统管理员 我也可以控制代理计算机 我在网站服务器端使用 PHP5 I tried SERVER REMOTE ADDR 在 PHP 中 但此变
  • 处理 Mongoose 中的架构更改

    随着应用程序的发展 更新 迁移 Mongoose 模式的最佳实践 或工具 是什么 有趣的是 MongoDB 的诞生就是为了解决 RDBMS 中的模式问题 您不必迁移任何内容 您所要做的就是在架构定义中设置默认值 如果该字段是必需的 new
  • 如何管理 MySQL Workbench 中的 SQL 选项卡?

    我经常需要的每个项目都有多个 SQL 查询 我的问题是 我的所有项目都需要 Workbench 中相同的 MySQL 连接 所以我一直打开大量的 SQL 选项卡 如下所示 由于选项卡的数量超过了显示器的宽度 因此我必须左右滚动才能找到一些查
  • 无法使用 String#trim 作为 Array#map 的回调

    由于某种原因我无法使用String prototype trim call作为数组方法的回调 例如map or filter 在这种情况下 两个函数的工作原理相同 function trim string return string tri
  • 在 MySQL WorkBench 中打开现有数据库

    I got a DB files that created in My SQL and I want open them in My SQL WorkBench 6 1 The files I got contains FRM MYD MY
  • 查找数组中的最小值和最大值

    所以我试图找到用户输入的数组的最小值和最大值 这是我的代码 public static void main String args int a new int args length for int i 0 i lt args length
  • 匹配两个数据集中的 ID

    我有两组数据 包括前数据和后数据 受访者拥有唯一的 ID 我想创建一个子集 其中仅包含对两项调查做出回应的受访者 数据集示例 pre data lt data frame ID c 1 10 Y sample c yes no 10 rep
  • 有什么好的 CMS 可以与现有的 Java 网站集成(需要良好的 API)[关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我们有一个大型现有网站 用 Java Spring Hibernate JSP 编写 并希望添加一个内容
  • 提交表单时不要包含空参数

    我的控制器上的索引方法如下所示 public ActionResult Index string search string sort int groupId 对于搜索功能 我有以下形式 using Html BeginForm div H
  • jni 和在 java 中使用 c++ new'ed 对象

    我有一个与数据库对话的 C 层 这个 C 层执行一个新的 SomeObject 并将其返回给 java 我什么时候可以安全地通过 clean jni 调用删除 SomeObject 我可以在java返回对象后立即删除还是需要复制该对象然后删
  • Elm 中的数组与列表

    我很惊讶地得知Array and ListElm 中有两种不同的类型 Array http package elm lang org packages elm lang core 4 0 1 Array List http package
  • Android 位图图像大小

    我正在从网络下载图像 并使用图库小部件来显示图像 如果下载的图像很大 我的应用程序会崩溃并显示以下日志 E GraphicsJNI 3378 VM won t let us allocate 5591040 bytes 仅当图像大小达到会使
  • 在 C# 中创建 datagridview 表单

    我是 C 和 Windows 窗体应用程序的新手 现在 我想在表单中创建一个 Datagridview 我想用业务对象的属性填充其行 我按照此 msdn 页面中的示例进行操作 如何 将对象绑定到 Windows 窗体 DataGridVie
  • PHP 在冒号之前的单词上分割字符串

    我有一个看起来像这样的字符串 aaaaa lorem ipsum bb dolor sit amet ccc no pro movet 将字符串拆分为数组并在 PHP 中获得以下结果的最佳方法是什么 array 0 aaaaa lorem
  • 使用 telegram 的 TL 模式语言处理“标志”类型

    我编写了一个 tl 解析器 因此现在可以使用最新的层 53 但我不确定如何处理 标志 类型 它们仅在 tl 文档中提到 但在页面底部没有定义 据我所知 link https core telegram org mtproto TL form