Skip to content

Commit

Permalink
add basic operation examples
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Jul 10, 2024
1 parent 71d983e commit afe6b52
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,99 @@ func main() {
}
```

## Basic Operations
Here are some quick examples on basic operations like sending a message, media etc.

Naming convention:
- `ctx` is a `*ext.Context` object returned as a paramter in all update handlers.
- `update` is a `*ext.Update` object returned as a parameter in all update handlers.
- `chatId` is the chat id of the chat you want to send the message to. (type int64)

**Note**: You do not need to specify the peer field in the request, it is automatically filled by the library.

### Sending a Message
```go
ctx.SendMessage(chatId, &tg.MessagesSendMessageRequest{
Message: "Hello, World!",
// Peer: ... (No need of setting peer as we have passed chatId)
})
```

### Uploading media on telegram
If you want to send a local file, you will need to upload it to telegram using an uploader instance as we've done below for `test.jpg`:
```go
f, err := uploader.NewUploader(ctx.Raw).FromPath(ctx, "test.jpg")
if err != nil {
panic(err)
}
```

### Sending uploaded media to a chat
Let's upload the photo (`test.jpg`) we just uploaded on telegram:
```go
ctx.SendMedia(chatId, &tg.MessagesSendMediaRequest{
Message: "This is your caption",
Media: &tg.InputMediaUploadedPhoto{
File: f,
},
})
```

_For media types other than photos, use `tg.InputMediaUploadedDocument`._

### Retrieving a photo from a message and sending it
If you want to send a photo from a message, you can do it like this:
```go
m := update.EffectiveMessage
// we recommend you to check if the media is a photo casting it in real life applications.
photo := m.Media.(*tg.MessageMediaPhoto).Photo.(*tg.Photo)
ctx.SendMedia(chatId, &tg.MessagesSendMediaRequest{
Media: &tg.InputMediaPhoto{
// Specifying ID, AccessHash and FileReference of the photo is compulsory.
ID: &tg.InputPhoto{
ID: photo.ID,
AccessHash: photo.AccessHash,
FileReference: photo.FileReference,
},
},
})
```

### Sending a file to a chat after retrieving it from a message
```go
m := update.EffectiveMessage
// we recommend you to check if the media is a photo casting it in real life applications.
doc := m.Media.(*tg.MessageMediaDocument).Document.(*tg.Document)
ctx.SendMedia(chatId, &tg.MessagesSendMediaRequest{
Media: &tg.InputMediaDocument{
ID: &tg.InputDocument{
ID: doc.ID,
AccessHash: doc.AccessHash,
FileReference: doc.FileReference,
},
},
})
```

### Working with raw tl functions
Telegram has a big library of functions, Gotgproto doesn't have helper for all of them currently, but you can use the raw functions to call any function you want and also utilize this library's features. Here is an example of calling the `messages.getHistory` function to get chat history:
```go
// peer storage is managed by the library automatically with each session. It stores the chat ids and their access hash which are needed to create input peer queries.
peerStorage = ctx.PeerStorage
// get the peer from the chat id
inputPeer := peerStorage.GetInputPeerById(chatId)
// draw out a raw function call using ctx.Raw api
ctx.Raw.MessagesGetHistory(
ctx,
&tg.MessagesGetHistoryRequest{
// Peer is compulsory
Peer: inputPeer,
Limit: 10,
},
)
```


## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Expand Down

0 comments on commit afe6b52

Please sign in to comment.