Opening a LINE account and setting a Webhook URL
How to set up API Gateway
A mechanism to set "Server IP Whitelist" on the Line Message API side and authenticate on the AWS side as well
Signature verification to verify that the request was sent from LINE
Mechanism to get Channel Access Token set by Line from Lambda environment variable
LINE Message API
Open an account
Webhook URL setting
AWS API Gateway
Resource creation
Link the created resource with the Lambda Function
Lambda Function
3.1. Create Input class to receive JSON by POJO 3.2. Create Output class 3.3. Get replyToken and text from Input and store in Output. In addition, the information required for Reply Message is also set. 3.4. Convert Output to JSON 3.5. Generate and send a request 3.6. Whole code
Since the following JSON object is sent, the Input class is also constructed accordingly.
{
"events": [
{
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
"type": "message",
"timestamp": 1462629479859,
"source": {
"type": "user",
"userId": "U206d25c2ea6bd87c17655609a1c37cb8"
},
"message": {
"id": "325708",
"type": "text",
"text": "Hello, world"
}
},
{
"replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
"type": "follow",
"timestamp": 1462629479859,
"source": {
"type": "user",
"userId": "U206d25c2ea6bd87c17655609a1c37cb8"
}
}
]
}
Input.java
package jp.linebot;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Input {
private Events[] events;
}
Events.java
package jp.linebot;
import org.joda.time.DateTime;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Events {
private String replyToken;
private String type;
private Long timestamp;
private Source source;
private Message message;
}
Source.java
package jp.linebot;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Source {
private String type;
private String userId;
}
Message.java
package jp.linebot;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Message {
private String id;
private String type;
private String text;
}
Output.java
package jp.linebot;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
@Data
public class Output {
private String replyToken;
private List<Messages> messages = new ArrayList<>();
}
Messages.java
package jp.linebot;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Messages {
private String type;
private String text;
}
Output output = new Output();
output.setReplyToken(input.getEvents()[0].getReplyToken());
Messages outMessage = new Messages();
outMessage.setType("text");
outMessage.setText(input.getEvents()[0].getMessage().getText() + "?");
output.getMessages().add(outMessage);
Gson gson = new Gson();
context.getLogger().log(gson.toJson(output));
httpPost = new HttpPost("https://api.line.me/v2/bot/message/reply");
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Authorization", "Bearer " + "{Channel Access Token}");
StringEntity entity = new StringEntity(gson.toJson(output), StandardCharsets.UTF_8);
httpPost.setEntity(entity);
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse resp = client.execute(httpPost);
BufferedReader br = new BufferedReader(new InputStreamReader(resp.getEntity().getContent(), StandardCharsets.UTF_8)))
{
int statusCode = resp.getStatusLine().getStatusCode();
switch (statusCode) {
case 200:
br.readLine();
break;
default:
}
} catch (final ClientProtocolException e) {
} catch (final IOException e) {
}
return null;
LambdaFunctionHandler.java
package jp.linebot;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.google.gson.Gson;
public class LambdaFunctionHandler implements RequestHandler<Input, Object> {
@Override
public Object handleRequest(Input input, Context context) {
// TODO Auto-generated method stub
context.getLogger().log("token : " + input.getEvents()[0].getReplyToken());
context.getLogger().log("text : " + input.getEvents()[0].getMessage().getText());
Output output = new Output();
output.setReplyToken(input.getEvents()[0].getReplyToken());
Messages outMessage = new Messages();
outMessage.setType("text");
outMessage.setText(input.getEvents()[0].getMessage().getText() + "?");
output.getMessages().add(outMessage);
HttpPost httpPost = new HttpPost("https://api.line.me/v2/bot/message/reply");
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Authorization", "Bearer " + "{Channel Access Token}");
Gson gson = new Gson();
context.getLogger().log(gson.toJson(output));
StringEntity entity = new StringEntity(gson.toJson(output), StandardCharsets.UTF_8);
httpPost.setEntity(entity);
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse resp = client.execute(httpPost);
BufferedReader br = new BufferedReader(new InputStreamReader(resp.getEntity().getContent(), StandardCharsets.UTF_8)))
{
int statusCode = resp.getStatusLine().getStatusCode();
switch (statusCode) {
case 200:
br.readLine();
break;
default:
}
} catch (final ClientProtocolException e) {
} catch (final IOException e) {
}
return null;
}
}
Recommended Posts