mtproto icon indicating copy to clipboard operation
mtproto copied to clipboard

Question. How to send media to chat

Open LazarenkoA opened this issue 4 years ago • 2 comments

Please help with an example of sending a photo to an arbitrary chat. Here's what I found myself

client.MessagesSendMedia(&telegram.MessagesSendMediaParams{
	Silent:       false,
	Background:   false,
	ClearDraft:   false,
	Peer:         &telegram.InputPeerUser{ // user data
		UserId:     2121212,
		AccessHash: 212121211221,
	},
	ReplyToMsgId: 0,
	Media:        ???, 
	Message:      "бебе",
	RandomId:     rand.Int63(),
	ReplyMarkup:  nil,
	Entities:     nil,
	ScheduleDate: 0,
})

I don't understand what should be in Media, the field itself is an interface, implementing structure InputMediaUploadedPhoto, it, in turn, has a File property, an interface too, implementing InputFileObj, it's not clear how to fill its properties, I tried it like this:

media, err := client.MakeRequest(&telegram.InputMediaUploadedPhoto{
		File:       &telegram.InputFileObj{
			Id:          rand.Int63(),
			Parts:       -1,
			Name:        "photo_2020-10-24_10-46-53.jpg",
			Md5Checksum: md5,
		},
		Stickers:   nil,
		TtlSeconds: 0,
})

get an error INPUT_METHOD_INVALID_505969924_257016

LazarenkoA avatar Oct 25 '20 18:10 LazarenkoA

Hey @LazarenkoA! Thanks for asking!

I DON'T KNOW))))) Need to dig out, may be i'll add few support methods for this issue

quenbyako avatar Nov 04 '20 18:11 quenbyako

@LazarenkoA Hai, I have an example for you. Further more check out Uploading and Downloading Files

Goal : Send bot as bot.txt file given client and inputPeer

func Example(client *telegram.Client, inputPeer *telegram.InputPeer){
  fileID := 1234 // must be random
  fileData := []byte("bot") // your file data in []byte and follow rules from telegram api
  client.UploadSaveFilePart(fileID, 0, fileData)
  client.MessagesSendMedia(&telegram.MessagesSendMediaParams{
    RandomID: 1234125 // must be random,
    Peer: inputPeer,
    Media: &telegram.InputMediaUploadedDocument{
      Attribtues: []telegram.DocumentAttribute{
        &telegram.DocumentAttributeFilename{FileName: "bot.txt"},
      },
      File: &telegram.InputFileObj{
        ID: fileID,
        Parts: 1,
        Name: "bot.txt",
      },
    },
  })
}

azamaulanaaa avatar Jun 11 '21 03:06 azamaulanaaa