Skip to content

Instantly share code, notes, and snippets.

@sleroq
Created July 25, 2023 06:07
Show Gist options
  • Select an option

  • Save sleroq/7b0ef3957d50e67c4c1e7cb3e20c2418 to your computer and use it in GitHub Desktop.

Select an option

Save sleroq/7b0ef3957d50e67c4c1e7cb3e20c2418 to your computer and use it in GitHub Desktop.

Revisions

  1. sleroq created this gist Jul 25, 2023.
    116 changes: 116 additions & 0 deletions send_document.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@
    package main

    import (
    "fmt"
    "github.com/go-faster/errors"
    "github.com/gotd/td/telegram/message"
    "github.com/gotd/td/telegram/message/html"
    "github.com/gotd/td/telegram/uploader"
    "log"
    "os"

    "github.com/anonyindian/gotgproto"
    "github.com/anonyindian/gotgproto/dispatcher/handlers"
    "github.com/anonyindian/gotgproto/dispatcher/handlers/filters"
    "github.com/anonyindian/gotgproto/ext"
    "github.com/anonyindian/gotgproto/sessionMaker"
    "github.com/gotd/td/tg"
    )

    func main() {
    // Type of client to login to, can be of 2 types:
    // 1.) Bot (Fill BotToken in this case)
    // 2.) User (Fill Phone in this case)
    clientType := gotgproto.ClientType{
    BotToken: "BOT_TOKEN_HERE",
    }

    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
    clientType,
    // Optional parameters of client
    &gotgproto.ClientOpts{
    Session: sessionMaker.NewSession("echobot", sessionMaker.Session),
    },
    )
    if err != nil {
    log.Fatalln("failed to start client:", err)
    }

    dispatcher := client.Dispatcher

    // Command Handler for /file
    dispatcher.AddHandler(handlers.NewCommand("file", file))
    // This Message Handler will call our echo function on new messages
    dispatcher.AddHandlerToGroup(handlers.NewMessage(filters.Message.Text, echo), 1)

    fmt.Printf("client (@%s) has been started...\n", client.Self.Username)

    client.Idle()
    }
    func echo(ctx *ext.Context, update *ext.Update) error {
    _, err := ctx.Reply(update, update.EffectiveMessage.Message.Message, nil)
    return err
    }

    func file(ctx *ext.Context, _ *ext.Update) error {
    picture, err := os.ReadFile("picture.png")
    if err != nil {
    return errors.Wrap(err, "opening file")
    }

    upldr := uploader.NewUploader(ctx.Raw)
    sender := message.NewSender(ctx.Raw).WithUploader(upldr)

    file, err := upldr.FromBytes(ctx, fmt.Sprintf("picture.png"), picture)
    if err != nil {
    return errors.Wrap(err, "uploading file to telegram")
    }

    document := message.UploadedDocument(file,
    html.String(nil, `Upload: <b>From bot</b>`),
    )

    document.
    MIME("image/jpeg").
    Filename(fmt.Sprintf("picture.png"))

    uploadedShit, err := sender.Self().UploadMedia(ctx, document)
    if err != nil {
    return errors.Wrap(err, "uploading media")
    }

    var media tg.MessageMediaDocument
    switch v := uploadedShit.(type) {
    case *tg.MessageMediaDocument: // messageMediaDocument#4cf4d72d
    media = *v
    default:
    return errors.Wrap(err, "unexpected media type")
    }

    var doc tg.Document
    switch v := media.Document.(type) {
    case *tg.Document: // document#8fd4c4d8
    doc = *v
    default:
    return errors.Wrap(err, "unexpected document type")
    }

    location := tg.InputDocumentFileLocation{
    ID: doc.ID,
    AccessHash: doc.AccessHash,
    FileReference: doc.FileReference,
    ThumbSize: "",
    }

    _, err = sender.Self().Album(ctx.Context, message.Document(&location))
    if err != nil {
    return errors.Wrap(err, "sending document")
    }

    return err
    }