Skip to content

Message Context

Alper Kürşat edited this page Feb 14, 2022 · 1 revision

Understanding Message Context

Contexts are simple Turing Machines that manages consecutive sends.

Whenever send forward reply and copy method called, a new message context created and attached as related key. (.send() -> .sent)

Method Created Context
reply() .replied
send() .sent
forward() .forwarded
copy() .copied
delete() No context

Let's assume that we have an incoming message(A), if we call res.reply(), we send a reply to A. This creates a new context at .replied (B).

If we call .reply again (res.reply().reply()) we reply to the original message(A) again.

But if we call .reply to the .replied (res.reply().replied.reply()), we reply to our own message(B).

Example Code Context
res A
res.reply() A
res.reply().reply() A
res.reply().replied A:B
res.reply().sent Error
res.reply().forward().forwarded A:C
res.reply().replied.forward().forwarded A:D:E

Context Diagram

But. How can I reply my message if it hasn't sent yet?

No matter you write chained methods, Botocrat handles async part and waits for the sent message before sending a new message in created context.

How can I get sent message?

As diagram shows above, you can get the sent message via .then method.

...
res.reply("This is a reply")
  .replied.then((reply) => {
    console.log(reply.text) // "This is a reply"
  })
...