I decided to create an app that automatically sends messages with LINE, so I decided to use the LINE API. I started working thinking that I could send a message with curl. As a result, I was able to send a message with curl using LINE_API + AWS (APIGateway + lambda). I wanted to make a note of the know-how at that time, and I personally didn't get the information that was already there, so I posted it.
First, create a LINE account normally. LINE can create an account without registering an "ID" and "password" in the system, but please register an "ID" and "password" to create a LINE account. If you have already created it, this work is unnecessary.
Next, create a BOT. After all, create an account for development. When you look at the official website, you will see terms such as BOT, provider, and channel. Those who understand the details can clearly distinguish it, but I could only understand it somehow, probably because of my lack of understanding. In conclusion, you can think of all accounts as ≒ (nearly equal) accounts at first.
https://developers.line.biz/ja/docs/messaging-api/overview/
Then add the provider to your provider list. It's okay for you to understand that creating a new LINE account for automatic sending and replying.
https://developers.line.biz/console/channel/1653419974/basic/
Once created, make sure you can chat with your development account. There is a QR code at the bottom of the screen, so let's invite to talk with your first account you created.
After confirming the operation of the BOT, set the URL of the webhook event to send a message to the existing chat room. The reason why this work is necessary is that when sending a message, the user ID and room ID of the destination are required. It was said that these cannot be obtained by normal LINE user operations, and cannot be easily obtained using the LINE app.
Change the part of the red frame above.
What is a webhook event? It is a function that sends a fixed message to the set URL when a message is received.
This is where AWS API Gateway + lambda comes in. Set the URL of API Gateway in the webhook URL.
The source of lambda should be able to use the sample below as it is. It's python.
lambda_function.py
import json
import traceback
# ==================================================
#The first function to be called on lambda
# ==================================================
def lambda_handler(event, context):
try:
#Log output of request contents
log(event, 'receive request.')
createResponse()
except:
#When an unexpected error occurs
logger.log(traceback.format_exc())
traceback.print_exc()
return createResponse()
# ==================================================
#Generate a response
# ==================================================
def createResponse():
return {
'isBase64Encoded': False,
'statusCode': 200,
'headers': {},
'body': {}
}
# ==================================================
#Log output
# ==================================================
def log(event, msg):
print('EVENT >> ' + str(event))
print('LOG >> ' + str(msg))
API Gateway settings look like this
The log is output to cloudwatch. UserId and roomId should be output in this. You can specify these as destinations.
Various things have come out. I tried to make it easy to understand. Strictly speaking, there are many different points, but I think it's easy to understand if I think this way based on my own judgment and prejudice, so I created it.
① http telegram is sent from the mobile phone (= message is sent by LINE) ② The server sends the received message to the mobile phone / BOT in the room. ③ POST telegram is sent to the URL registered as the destination URL of the webhook in BOT.
Finally, I will send a message with curl. Sending a message on LINE is called push, and since the API is open to the public, send it according to this. https://developers.line.biz/ja/reference/messaging-api/#send-push-message
By the way, I used a chrome extension called ASR. Anything that can send an http telegram will do.
Method POST URL https://api.line.me/v2/bot/message/push Content-Type application/json Authorization Bearer Issued authorization Token
{
"to":Obtained ID,
"messages":[
{
"type":"text",
"text":"You can send it automatically like this."
}
]
}
Recommended Posts