tomquirk / linkedin-api

👨‍💼Linkedin API for Python
MIT License
1.73k stars 402 forks source link

send message with attachment #196

Open MaherBTA opened 2 years ago

MaherBTA commented 2 years ago

Hi,

Is there a way to send a message with attachment. I see the attachments field is empty in the payload. what should I put in it ?

{
    "keyVersion": "LEGACY_INBOX",
            "conversationCreate": {
                "eventCreate": {
                    "value": {
                        "com.linkedin.voyager.messaging.create.MessageCreate": {
                            "attributedBody": {
                                "text": "text",
                                "attributes": []
                            },
                            "attachments": []
                        }
                    }
                },
                "subtype": "MEMBER_TO_MEMBER",
                "recipients": ["id"]
            }
    }
abinpaul1 commented 2 years ago

AFAIK its not implemented yet. I think it invloves a a two step process, upload the file and get an urnID for the same and send those attachments in the request.

abinpaul1 commented 2 years ago

POST https://www.linkedin.com/voyager/api/voyagerMediaUploadMetadata?action=upload

{
    "mediaUploadType": "MESSAGING_PHOTO_ATTACHMENT",
    "fileSize": 5096234,
    "filename": "Screenshot 2021-11-06 at 5.26.53 PM.png"
}

This would return a json of the form

{
    "data": {
        "value": {
            "urn": "urn:li:digitalmediaAsset:C5606AQFlP7MTkIqRbA",
            "mediaArtifactUrn": "urn:li:digitalmediaMediaArtifact:(urn:li:digitalmediaAsset:C5606AQFlP7MTkIqRbA,urn:li:digitalmediaMediaArtifactClass:messaging-attachmentFile)",
            "recipes": [
                "urn:li:digitalmediaRecipe:messaging-attachment"
            ],
            "singleUploadHeaders": {},
            "assetRealtimeTopic": "urn:li-realtime:digitalmediaAssetUpdatesTopic:urn:li:digitalmediaAsset:C5606AQFlP7MTkIqRbA",
            "singleUploadUrl": "https://www.linkedin.com/dms-uploads/C5606AQFlP7MTkIqRbA/messaging-attachmentFile/0?ca=vector_messaging&cn=uploads_encrypted&f=U2NyZWVuc2hvdCsyMDIxLTExLTA2K2F0KzUuMjYuNTMrUE0ucG5n&sync=1&v=beta&ut=2Xi0Gk6BxBoq41",
            "type": "SINGLE",
            "$type": "com.linkedin.mediauploader.MediaUploadMetadata"
        },
        "$type": "com.linkedin.restli.common.ActionResponse"
    },
    "included": []
}

After this , we need to make a PUT request with data to singleUploadUrl

PUT https://www.linkedin.com/dms-uploads/C5606AQFlP7MTkIqRbA/messaging-attachmentFile/0?ca=vector_messaging&cn=uploads_encrypted&f=U2NyZWVuc2hvdCsyMDIxLTExLTA2K2F0KzUuMjYuNTMrUE0ucG5n&sync=1&v=beta&ut=2Xi0Gk6BxBoq41

After that you could add attachments like below

"attachments": [
                    {
                        "id": "urn:li:digitalmediaAsset:C5606AQFlP7MTkIqRbA",
                        "name": "Screenshot 2021-11-06 at 5.26.53 PM.png",
                        "byteSize": 5096234,
                        "mediaType": "image/png",
                        "reference": {
                            "string": "blob:https://www.linkedin.com/3bab8027-2344-40ac-8eb9-9fbaa142e3e7"
                        }
                    }
                ]

I am not sure where the "Reference"["string"] is got from. I think it should be the response when the PUT request is successfull.

MaherBTA commented 2 years ago

what should be the payload of the PUT request to singleUploadUrl?

abinpaul1 commented 2 years ago

I believe it must be the attachment file. So, the body of the PUT request would contain the binary file.

MaherBTA commented 2 years ago

I get error 400 : bad request when I send a PUT request with a form-data file.

abinpaul1 commented 2 years ago

Sorry for the delay in response. I checked again.

The body should be the binary data directly. Dont use form-data. I think it would be something like this https://stackoverflow.com/a/34861614/12306553.

Also it seems, you can omit the "reference": { "string": "blob:https://www.linkedin.com/3bab8027-2344-40ac-8eb9-9fbaa142e3e7" } while sending the message and it still works.

MaherBTA commented 2 years ago

Big thanks that worked for me!

abinpaul1 commented 2 years ago

That's great.🥳 Glad to know it worked.

We'll keep this issue open, incase someone is interested in implementing this functionality in this project.