GoTGProto is a helper package for gotd library, It aims to make td's raw functions easy-to-use with the help of features like using session strings, custom helper functions, storing peers and extracting chat or user ids through it etc.
We have an outstanding userbot project going on with GoTGProto, you can check it out by clicking here.
You can use this package to create bots and userbots with Telegram MTProto easily in golang, for any futher help you can check out the documentations or reach us through the following:
Note: This library is in the beta stage yet and may not be stable for every case.
You can download the library with the help of standard go get
command.
go get github.com/celestix/gotgproto
You can find various examples in the examples' directory, one of them i.e. authorizing as a user is as follows:
package main
import (
"log"
"github.com/celestix/gotgproto"
"github.com/celestix/gotgproto/sessionMaker"
"github.com/glebarez/sqlite"
)
func main() {
client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
gotgproto.ClientTypePhone("PHONE_NUMBER_HERE"),
// Optional parameters of client
&gotgproto.ClientOpts{
Session: sessionMaker.SqlSession(sqlite.Open("echobot")),
},
)
if err != nil {
log.Fatalln("failed to start client:", err)
}
client.Idle()
}
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.
ctx.SendMessage(chatId, &tg.MessagesSendMessageRequest{
Message: "Hello, World!",
// Peer: ... (No need of setting peer as we have passed chatId)
})
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
:
f, err := uploader.NewUploader(ctx.Raw).FromPath(ctx, "test.jpg")
if err != nil {
panic(err)
}
Let's upload the photo (test.jpg
) we just uploaded on telegram:
ctx.SendMedia(chatId, &tg.MessagesSendMediaRequest{
Message: "This is your caption",
Media: &tg.InputMediaUploadedPhoto{
File: f,
},
})
For media types other than photos, use tg.InputMediaUploadedDocument
.
media := &tg.InputMediaUploadedDocument{
File: f,
MimeType: "audio/mp4", // or any other mime type like "video/mp4" for videos, "audio/mp4" for audios etc.
Thumb: f, // Optional, you can set it to nil if you don't want to set a thumbnail.
Attributes: []tg.DocumentAttributeClass{&tg.DocumentAttributeFilename{FileName: f.GetName()}},
}
ctx.SendMedia(chatID, &tg.MessagesSendMediaRequest{
Media: media,
Message: "This is your caption"
})
If you want to send a photo from a message, you can do it like this:
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,
},
},
})
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,
},
},
})
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:
// 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,
},
)
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update the examples as appropriate.
Licensed Under GNU General Public License v3