Conversation
Its hard to keep track of all the conversations you have with a peer, and retrieve their responses. Conversation struct is a simple way to keep track of all the messages exchanged between two peers.
- Finite State Machine (FSM) based conversation handling.
Quick Example
package main
import (
"github.com/amarnathcjd/gogram/telegram"
)
func main() {
// Create a new client
client := telegram.NewClient(&telegram.ClientConfig{
AppID: 12345,
AppHash: "0123456789abcdef0123456789abcdef",
})
// Create a new conversation
conv := telegram.NewConversation(client, 1234567890)
// Send a message
conv.SendMessage("Hello, World!")
// Receive a message
msg := conv.GetResponse()
if msg != nil {
// Print the message
fmt.Println(msg.Text())
}
}
Usage
To create a new conversation, you need to call the NewConversation
function with the client and the peer's ID as arguments.
conv := telegram.NewConversation(client, 1234567890)
To send and receive messages, you can use the following methods:
-
SendMessage
: Sends a message to the peer. -
SendMedia
: Sends a media file to the peer. -
MarkRead
: Marks the last message received from the peer as read. -
GetResponse
: Receives a message from the peer, be it reply or not. -
GetReply
: Receives a reply to the last message sent by the peer. -
GetEdit
: Receives an edited message from the peer. -
WaitRead
: Waits for the peer to read the last message sent by the client. -
WaitEvent
: Waits for a custom event from the peer. -
Close
: Closes the conversation and stops listening for messages.
Example
conv := telegram.NewConversation(client, 1234567890, 200) // 200 depicts the timeout in seconds
conv.SendMessage("Hello, World!")
msg := conv.GetResponse()
if strings.Contains(msg.Text(), "Hello") {
conv.SendMessage("Hi!")
}
conv.WaitRead()
conv.SendMessage("Reply Me!")
conv.GetReply()
conv.Close()
Note
- The
GetResponse
,GetReply
, andGetEdit
methods returnnil
and log an error if the timeout is reached.