Bot - メッセージの送信
Bot からのメッセージ送信方法は、ユーザー指定とトークルーム指定の2つがあります。
-
メッセージの送信 - ユーザー指定
POST/bots/{botId}/users/{userId}/messages -
メッセージの送信 - トークルーム指定
POST/bots/{botId}/channels/{channelId}/messages
グループや組織のトークルームにメッセージを送信するにはトークルーム指定を使用します。
Java Sample Code
標準ライブラリを使用したサンプルコードです。下記はユーザー指定でのメッセージ送信です。
ApiAuthInfo
, BotMessageSend
クラスに認証情報などの必要事項を記載します。
ApiRequest.java
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
class ApiAuthInfo {
// API Access Token ***最初に "Bearer " を必ず付ける***
String accessToken = "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==";
public String getAccessToken() {
return this.accessToken;
}
}
class BotMessageSend extends ApiAuthInfo {
// メッセージを送信する Bot の ID
String botId = "1234567890";
// メッセージ送信先ユーザーの userId
String id = "xxxxx-xxxxxxxxx-xxxxxx";
// Request Uri
String uri = "https://www.worksapis.com/v1.0/bots/" + this.botId + "/users/" + this.id + "/messages";
// Request Body
String body = "{\"content\": {\"type\": \"text\", \"text\": \"test\" }}";
public String getUri() {
return this.uri;
}
public String getBody() {
return this.body;
}
}
public class ApiRequest {
public static void main(String[] args) {
HttpClient client = HttpClient.newHttpClient();
BotMessageSend ApiRequest = new BotMessageSend();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(ApiRequest.getUri()))
.headers("Authorization", ApiRequest.getAccessToken())
.headers("Content-Type","application/json")
.POST(HttpRequest.BodyPublishers.ofString(ApiRequest.getBody()))
.build();
try {
// リクエストを送信
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
// レスポンスボディを出力
System.out.println(response.body());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
Request Body Sample
サンプルコードの ApiRequest
クラスの body
を変更することにより、様々なタイプのメッセージを送信することができます。
type: text
String body = "{\"content\": {\"type\": \"text\", \"text\": \"test\" }}";
type: image
String body = "{\"content\": {\"type\": \"image\", \"previewUrl\": \"https://developers.worksmobile.com/favicon.png\", \"resourceUrl\": \"https://developers.worksmobile.com/favicon.png\" }}";
type: link
String body = "{\"content\": {\"type\": \"link\", \"contentText\": \"worksmobile homepage\", \"linkText\": \"linkText\", \"link\": \"http://www.worksmobile.com\" }}";
type: sticker
String body = "{\"content\": {\"type\": \"sticker\", \"packageId\": \"1\", \"stickerId\": \"3\" }}";
type: button_template
String body = "{\"content\": {\"type\": \"button_template\", \"contentText\": \"button template\", \"actions\": [{ \"type\": \"message\", \"label\": \"Message lable\", \"text\": \"Message text\" },{ \"type\": \"message\", \"label\": \"Postback lable\", \"text\": \"Postback text\", \"postback\": \"postback data\" },{ \"type\": \"uri\", \"label\": \"LINE WORKS\", \"uri\": \"https://line.worksmobile.com/jp/\" }]}}";
type: list_template
String body = "{\"content\": {\"type\": \"list_template\",\"coverData\": {\"backgroundImage\": \"https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png\",\"title\": \"title\",\"subtitle\": \"subtitle\"},\"elements\": [{\"title\": \"WorksMobile's Homepage\",\"subtitle\": \"Link action subtitle\",\"image\": \"https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png\",\"action\": { \"type\": \"uri\", \"label\": \"Visit\", \"uri\": \"https://line.worksmobile.com\"}}, {\"title\": \"List Temaplate postback\",\"subtitle\": \"postback subtitle\",\"image\": \"https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png\",\"action\": { \"type\": \"message\", \"label\": \"Postback lable\", \"text\": \"Postback text\", \"postback\": \"postback data\"}}],\"actions\": [[{\"type\": \"message\",\"label\": \"下部ボタン Actions\",\"text\": \"下部ボタン Actions\",\"postback\": \"bottom button actions postback\"}]]}}";
type: carousel
String body = "{\"content\": {\"type\": \"carousel\",\"imageAspectRatio\": \"square\",\"columns\": [{\"thumbnailImageUrl\": \"https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png\",\"title\": \"carousel title 1\",\"text\": \"roomId carousel text\",\"defaultAction\": {\"type\":\"uri\",\"label\":\"Visit\",\"uri\":\"https://www.naver.com\"},\"actions\": [{\"type\": \"postback\",\"label\": \"carousel Postback\",\"data\": \"carousel postback data\",\"displayText\": \"Postback\"},{\"type\": \"message\",\"label\": \"carousel message\",\"text\": \"carousel message\"},{\"type\": \"uri\",\"label\": \"LINE WORKS\",\"uri\": \"https://line.worksmobile.com/jp/\"}]},{\"thumbnailImageUrl\": \"https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png\",\"title\": \"carousel title 2\",\"text\": \"carousel text 2\",\"defaultAction\": {\"type\":\"uri\",\"label\":\"Visit\",\"uri\":\"https://www.naver.com\"},\"actions\": [{\"type\": \"message\",\"label\": \"carousel Actions\",\"text\": \"carousel Actions\",\"postback\": \"carousel actions postback\"}]}]}}"
type: image_carousel
String body = "{\"content\": {\"type\": \"image_carousel\",\"columns\": [{\"imageUrl\": \"https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png\",\"action\": {\"type\": \"message\",\"label\": \"imgCarousel1\",\"text\": \"image carousel message\"}},{\"imageUrl\": \"https://line.worksmobile.com/jp/wp-content/uploads/guidelines-img-17.png\",\"action\": {\"type\": \"message\",\"label\": \"imgCarousel2\",\"text\": \"image carousel message\"}}]}}"
type: file
String body = "{\"content\": {\"type\": \"file\", \"resourceUrl\": \"https://line.worksmobile.com/kr/wp-content/uploads/2020/05/user-start-guide-v2.0.pdf\" }}";
type: flex
String body = "{\"content\": {\"type\": \"flex\",\"altText\": \"this is a flexible template\",\"contents\": {\"type\": \"carousel\",\"contents\": [{\"type\": \"bubble\",\"size\": \"mega\",\"header\": {\"type\": \"box\",\"layout\": \"horizontal\",\"contents\": [{\"type\": \"text\",\"text\": \"WORKS MOBILE\",\"size\": \"lg\",\"color\": \"#00c73c\",\"weight\": \"bold\"}],\"backgroundColor\": \"#ffffff\"},\"body\": {\"type\": \"box\",\"layout\": \"vertical\",\"contents\": [{\"type\": \"box\",\"layout\": \"vertical\",\"contents\": [{ \"type\": \"image\", \"url\": \"https://i.ibb.co/tXyrDKh/11-matthew-operation-quality-manager.png\", \"size\": \"full\"}],\"width\": \"120px\",\"height\": \"120px\",\"cornerRadius\": \"180px\",\"borderWidth\": \"1px\",\"borderColor\": \"#22222210\",\"offsetStart\": \"75px\",\"offsetEnd\": \"75px\"},{\"type\": \"box\",\"layout\": \"vertical\",\"contents\": [{ \"type\": \"text\", \"text\": \"John Smith\", \"weight\": \"bold\", \"size\": \"xl\", \"align\": \"center\"},{ \"type\": \"text\", \"text\": \"Graphic Designer\", \"size\": \"sm\", \"align\": \"center\"}],\"margin\": \"xl\"},{\"type\": \"box\",\"layout\": \"vertical\",\"contents\": [{\"type\": \"box\",\"layout\": \"baseline\",\"contents\": [ {\"type\": \"text\",\"text\": \"Department\",\"size\": \"sm\",\"color\": \"#858f89\", \"flex\": 3,\"margin\": \"md\"},{\"type\": \"text\",\"text\": \"Product Design 1\", \"size\": \"sm\",\"margin\": \"md\",\"flex\": 5} ]},{\"type\": \"box\", \"layout\": \"baseline\", \"contents\": [{\"type\": \"text\",\"text\": \"Phone\",\"size\": \"sm\", \"color\": \"#858f89\",\"flex\": 3,\"margin\": \"md\"},{\"type\": \"text\",\"text\": \"000-111-2222\",\"size\": \"sm\",\"margin\": \"md\",\"flex\": 5} ], \"margin\": \"sm\"},{ \"type\": \"box\", \"layout\": \"baseline\", \"contents\": [{\"type\": \"text\", \"text\": \"E-mail\",\"size\": \"sm\",\"color\": \"#858f89\",\"flex\": 3, \"margin\": \"md\"},{\"type\": \"text\",\"text\": \"email@works.com\", \"size\": \"sm\",\"margin\": \"md\",\"flex\": 5} ], \"margin\": \"sm\"}],\"margin\": \"xxl\",\"width\": \"260px\",\"alignItems\": \"center\",\"paddingAll\": \"6px\"}]},\"footer\": {\"type\": \"box\",\"layout\": \"vertical\",\"contents\": [{\"type\": \"button\",\"action\": {\"type\": \"uri\",\"label\": \"Send a message\",\"uri\": \"https://worksmobile.com\"},\"style\": \"primary\"},{\"type\": \"button\",\"action\": {\"type\": \"uri\",\"label\": \"Call\",\"uri\": \"https://worksmobile.com\"},\"margin\": \"md\",\"style\": \"secondary\"}]}},{\"type\": \"bubble\",\"size\": \"mega\",\"header\": {\"type\": \"box\",\"layout\": \"horizontal\",\"contents\": [{\"type\": \"text\",\"text\": \"WORKS MOBILE\",\"size\": \"lg\",\"color\": \"#00c73c\",\"weight\": \"bold\"}],\"backgroundColor\": \"#ffffff\"},\"body\": {\"type\": \"box\",\"layout\": \"vertical\",\"contents\": [{\"type\": \"box\",\"layout\": \"vertical\",\"contents\": [{ \"type\": \"image\", \"url\": \"https://i.ibb.co/jrD8083/05-emma-business-maketing-manager.png\", \"size\": \"full\"}],\"width\": \"120px\",\"height\": \"120px\",\"cornerRadius\": \"180px\",\"borderColor\": \"#22222210\",\"borderWidth\": \"1px\",\"offsetStart\": \"75px\",\"offsetEnd\": \"75px\"},{\"type\": \"box\",\"layout\": \"vertical\",\"contents\": [{ \"type\": \"text\", \"text\": \"Sarah Harding\", \"weight\": \"bold\", \"size\": \"xl\", \"align\": \"center\"},{ \"type\": \"text\", \"text\": \"UX Designer\", \"size\": \"sm\", \"align\": \"center\"}],\"margin\": \"xl\"},{\"type\": \"box\",\"layout\": \"vertical\",\"contents\": [{ \"type\": \"box\", \"layout\": \"baseline\", \"contents\": [ { \"type\": \"text\", \"text\": \"Department\", \"size\": \"sm\", \"color\": \"#858f89\", \"flex\": 3, \"margin\": \"md\" }, { \"type\": \"text\", \"text\": \"Interface Design 1\", \"size\": \"sm\", \"margin\": \"md\", \"flex\": 5 } ]},{ \"type\": \"box\", \"layout\": \"baseline\", \"contents\": [ { \"type\": \"text\", \"text\": \"Phone\", \"size\": \"sm\", \"color\": \"#858f89\", \"flex\": 3, \"margin\": \"md\" }, { \"type\": \"text\", \"text\": \"333-444-5555\", \"size\": \"sm\", \"margin\": \"md\", \"flex\": 5 } ], \"margin\": \"sm\"},{ \"type\": \"box\", \"layout\": \"baseline\", \"contents\": [ { \"type\": \"text\", \"text\": \"E-mail\", \"size\": \"sm\", \"color\": \"#858f89\", \"flex\": 3, \"margin\": \"md\" }, { \"type\": \"text\",\"text\": \"email@works.com\", \"size\": \"sm\", \"margin\": \"md\",\"flex\": 5 } ], \"margin\": \"sm\"}],\"margin\": \"xxl\",\"width\": \"260px\",\"alignItems\": \"center\",\"paddingAll\": \"6px\"}]},\"footer\": {\"type\": \"box\",\"layout\": \"vertical\",\"contents\": [{\"type\": \"button\",\"action\": {\"type\": \"uri\",\"label\": \"Send a message\",\"uri\": \"https://worksmobile.com\"},\"style\": \"primary\"},{\"type\": \"button\",\"action\": {\"type\": \"uri\",\"label\": \"Call\",\"uri\": \"https://worksmobile.com\"},\"margin\": \"md\",\"style\": \"secondary\"}]}}]}}}";
quickReply
クイックリプライ (quick reply) は今まで紹介したすべてのメッセージタイプで使用可能です。
使用するには、content
パラメータ内に quickReply
パラメータを設定します。
quickReply with type: text
String body = "{\"content\": {\"type\": \"text\",\"text\": \"quickReply\",\"quickReply\": {\"items\": [{\"action\": {\"type\": \"postback\",\"label\": \"Postback\",\"data\": \"postback data\",\"displayText\": \"Postback\"}},{\"action\": {\"type\": \"message\",\"label\": \"メッセージ\",\"text\": \"メッセージ\"}},{\"imageUrl\": \"https://developers.worksmobile.com/favicon.png\",\"action\": {\"type\": \"uri\",\"label\": \"LINE WORKS\",\"uri\": \"https://line.worksmobile.com/jp/\"}},{\"action\": {\"type\": \"camera\",\"label\": \"カメラ\"}},{\"action\": {\"type\": \"cameraRoll\",\"label\": \"カメラロール\"}},{\"action\": {\"type\": \"location\",\"label\": \"場所\"}}]}}}";
以上です。