处理"标志"电报的TL模式语言中的类型

时间:2016-08-11 18:07:28

标签: python api telegram

我写了一个tl解析器,所以现在可以使用最新的层(53)。但我不确定如何处理"标志"类型。它们仅在tl文档中提及但未在页面底部定义(据我所知):link

例如,当一个方法返回一条消息'键入它应该是这样的: str2[j] = star;

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

我的解析器会发出“消息”消息。像这样打字:

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;

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

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

1 个答案:

答案 0 :(得分:1)

  

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

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;

示例flags:# out:flags.1?true

解码标志:BinaryAND(标志,2 ^ ix)=== 2 ^ ix - &gt;这将帮助您确定是否包含字段

标志 = flags字段的值,这通常是带标记的对象的第一个字段

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

对于上面的示例,out如果true则值为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