LoginSignup
3
1

More than 3 years have passed since last update.

salesforceからLINEに画像を送りたい(Apex、MessagingAPI)

Last updated at Posted at 2020-01-15

前提

やりたいこと

  • salesforceのアプリから、LINEに画像を送りたい

どうすれば送信できるか

実装内容

  • 画像URL作成処理
ContentVersion conte = new ContentVersion(
    VersionData = bodyData, // アップロードした画像データが入る
    PathOnClient  = 'SendImage ' + System.now().format('MM/dd'), // 任意のファイル名
    Title = 'SendImage ' + System.now().format('MM/dd') // 任意のファイル名
);
insert conte;
ContentDistribution cdl = new ContentDistribution(
    ContentVersionId = conte.id, // ContentVesrionのid
    Name = 'SendImageUrl ' + System.now().format('MM/dd') // 任意のファイル名
);
insert cdl;
// レコードを作成してからDistributionPublicUrlを取得する
ContentDistribution cdlUrl = [SELECT DistributionPublicUrl FROM ContentDistribution WHERE Id = :cdl.Id LIMIT 1];
// 取得したurlを元に、画像のURLを作成する
Organization oid = [SELECT Id FROM Organization LIMIT 1];
// DistributionPublicUrlの「/a/」より後ろの文字列が必要
String dUrl = cdlUrl.DistributionPublicUrl.substringAfterLast('/a/');
String imageUrl = 'https://[domain]--c.documentforce.com/sfc/dist/version/renditionDownload?
rendition=ORIGINAL_Jpg&versionId=' + conte.Id + '&d=/a/' + dUrl + '&oid=' + oid.Id;

  • LINEへの送信処理
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.line.me/v2/bot/message/push');// MessagingAPIで指定されている送信時URL
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;');
request.setHeader('Authorization', 'Bearer ' + token); // チャネルtokenを後ろにつける
String body = '{"to": "[送信先のUserId]", "messages": [{"type":"image", "originalContentUrl":"[画像URL]", "previewImageUrl":"[画像URL]"}]}';
request.setBody(body);
HttpResponse response = http.send(request);

注意点

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1