在F#中,当字符串具有一些轻度标记属性时,如何按块分割字符串列表?
我有一个工具,它使用一个电报聊天机器人与它的用户交互 电报限制了呼叫速率,所以我使用了一个定期刷新的队列系统 因此,当前代码非常基本:在F#中,当字符串具有一些轻度标记属性时,如何按块分割字符串列表?,f#,telegram,F#,Telegram,我有一个工具,它使用一个电报聊天机器人与它的用户交互 电报限制了呼叫速率,所以我使用了一个定期刷新的队列系统 因此,当前代码非常基本: // flush the message queue let flushMessageQueue() = if not messageQueue.IsEmpty then <- messageQueue is a ConcurrentQueue // get all the messages let m
// flush the message queue
let flushMessageQueue() =
if not messageQueue.IsEmpty then <- messageQueue is a ConcurrentQueue
// get all the messages
let messages =
messageQueue
|> Seq.unfold(fun q ->
match q.TryDequeue () with
| true, m -> Some (m, q)
| _ -> None)
// put all the messages in a single string
let messagesString = String.Join("\n", messages)
// send the data
client.SendTextMessageAsync(chatId, messagesString, ParseMode.Markdown)
|> Async.AwaitTask
|> Async.RunSynchronously
|> ignore
但随着信息变得更加复杂,两个问题同时出现:
部分输出是带有简单标记的格式化文本:
- 某些行块在``段之间换行
- 在某些行中也有一些```部分
- 文本为UTF-8,使用了一组符号
```
this is a group of lines
with one, or many many lines
```
and sometimes there are things ```like this``` as well
而且。。。我发现电报也将消息大小限制为4kb
因此,我想到了两件事:
- 我可以使用open/close```来维护一个状态,并从队列中拉出,根据状态将每一行用三个回标记进行包装,然后推入另一个队列,该队列将用于生成4kb块
- 我可以继续从重新格式化的队列中获取消息并聚合它们,直到达到4kb,或者队列结束并循环
```
this is a group of lines
with one, or many many lines
```
and sometimes there are things ```like this``` as well