[PYTHON] [Supplement to the previous article] I tried using the PUSH API of LINE Bot, which had become available in the free plan before I knew it.

About this article

Supplement to the previous article "[Move LINE Bot implemented in Python (Flask)" without using Heroku "(https://qiita.com/hiro0236/items/84581c5e4481185d4a5c)". In the past, with the LINE Bot free plan, sending messages to users was limited to responding to messages sent by users. I couldn't use the PUSH API to send a message to the user from here, but before I knew it, I was able to use the PUSH API even with the free plan. As a result, the range of development has expanded even with the free plan, so we will investigate how to use it.

Why is it ready to use before you know it?

After investigating, it seems that there was a announcement like this from the LINE official. It seems that the PUSH API can be used even in the free plan from around June 2019. However, there is a limit of 1,000 per month **. A service with more than 100 users seems to be tough, but if it fits in a few tens of people (depending on the application), it seems to be sufficient.

In any case, there is no doubt that the range of development has greatly expanded for engineers developing with a free plan.

Push message type

[Official reference] According to (), there are the following four types.

--Push message (send a message to one user using the user ID as a key) --Multicast message (send message to multiple users using user ID as key) --Narrow cast message (send messages by narrowing down the users based on the attribute information of the users to be sent) --Broadcast message (send a message to all users who are friends with your bot account)

Try sending a message

You can hit the LINE API directly with curl etc., but since I installed line-bot-sdk-python in the previous article I'm going to write code to send a push message in Python. Implement while reading Implementation of line-bot-sdk-python.

Since it is a continuation of the previous article, we will proceed assuming that line-bot-sdk etc. have already been introduced.

Send push message

It can be realized with the following code. If you just send a message, you can easily implement it as follows.

push.py


from linebot import LineBotApi
from linebot.models import TextSendMessage

CHANNEL_ACCESS_TOKEN = "Set channel access token"
line_bot_api = LineBotApi(CHANNEL_ACCESS_TOKEN)

def main():
    to = "Specify user ID"
    messages = TextSendMessage(text="Push notification sending test")
    line_bot_api.push_message(to, messages=messages)

if __name__ == "__main__":
    main()

When I executed it, the following message was sent from the bot.

linebot3_1.png

Send multicast messages

I send messages to multiple users, but I only have one LINE account I haven't been able to confirm whether it will be sent to multiple people.

multicast.py


from linebot import LineBotApi
from linebot.models import TextSendMessage

CHANNEL_ACCESS_TOKEN = "Set channel access token"
line_bot_api = LineBotApi(CHANNEL_ACCESS_TOKEN)

def main():
    to = ["Specify the user ID to be sent as a list type"]
    messages = TextSendMessage(text="Multicast test")
    line_bot_api.multicast(to, messages=messages)

if __name__ == "__main__":
    main()

linebot3_2.png

Send narrowcast message

I didn't understand a little. It seems that the user's age and gender will be narrowed down, but it is unclear how the age and gender are set as user-specific information. You can also get this information by trying to get your profile with API to get user profile. Zu. As a future issue, I will investigate a little more.

Send broadcast message

I send a message to all of my bot's friends, but I'm still not sure because I'm the only bot's friend.

broadcast.py


from linebot import LineBotApi
from linebot.models import TextSendMessage

CHANNEL_ACCESS_TOKEN = "Set channel access token"
line_bot_api = LineBotApi(CHANNEL_ACCESS_TOKEN)

def main():
    messages = TextSendMessage(text="Broadcast test")
    line_bot_api.broadcast(messages=messages)

if __name__ == "__main__":
    main()

linebot3_3.png

Supports other than text messages

In addition to text messages, line-bot-sdk-python also supports sending stamps and images. I checked only stamps and images this time, but as far as I can see the implementation It seems that location information etc. can also be sent

Send stamp

See here for the package_id and sticker_id required to specify the stamp. Looking at the official accounts of companies, it seems that some stamps are sold at stamp shops. It is unknown how this is achieved. Investigate a little more.

push_sticker.py


from linebot import LineBotApi
from linebot.models import StickerSendMessage

CHANNEL_ACCESS_TOKEN = "Set channel access token"
line_bot_api = LineBotApi(CHANNEL_ACCESS_TOKEN)

def main():
    to = "Specify user ID"
    messages = StickerSendMessage(package_id='11537', sticker_id='52002734')
    line_bot_api.push_message(to, messages=messages)

if __name__ == "__main__":
    main()

linebot3_4.png

Send image

push_image.py


from linebot import LineBotApi
from linebot.models import ImageSendMessage

CHANNEL_ACCESS_TOKEN = "Set channel access token"
line_bot_api = LineBotApi(CHANNEL_ACCESS_TOKEN)

def main():
    to = "Specify user ID"
    messages = ImageSendMessage(original_content_url="https://pbs.twimg.com/profile_images/1201406146822557696/ewFFvnAa_400x400.jpg ", preview_image_url="https://pbs.twimg.com/profile_images/1201406146822557696/ewFFvnAa_400x400.jpg ")
    line_bot_api.push_message(to, messages=messages)

if __name__ == "__main__":
    main()

linebot3_5.png

in conclusion

In the previous article, I wrote a new article about the matters that I had personally set as future issues, but new issues have come up again. We will update the article or create a new one as the survey progresses.

I would appreciate it if you could let me know if there are any mistakes in the information I provided.

Recommended Posts

[Supplement to the previous article] I tried using the PUSH API of LINE Bot, which had become available in the free plan before I knew it.
Before I knew it, I couldn't use hyphens in the client id of GAE's Channel API.
P100-PCIE-16GB was added to the GPU of Google Colab before I knew it
I tried to touch the API of ebay
I tried to visualize the lyrics of GReeeen, which I used to listen to crazy in my youth but no longer listen to it.
I tried to extract the text in the image file using Tesseract of the OCR engine
I tried using the API of the salmon data project
I tried to automate the construction of a hands-on environment using IBM Cloud's SoftLayer API
[Python] I tried collecting data using the API of wikipedia
I tried to summarize the contents of each package saved by Python pip in one line
I tried to get the index of the list using the enumerate function
I looked at the meta information of BigQuery & tried using it
I tried using the checkio API
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried to get the batting results of Hachinai using image processing
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
The case that the installation of pip became easier before I knew it
I tried to summarize various sentences using the automatic summarization API "summpy"
I tried to extract and illustrate the stage of the story using COTOHA
I tried to get the movie information of TMDb API with Python
I tried to display the altitude value of DTM in a graph
I tried the common story of using Deep Learning to predict the Nikkei 225
Using COTOHA, I tried to follow the emotional course of Run, Melos!
I tried to make PyTorch model API in Azure environment using TorchServe
I wanted to know the number of lines in multiple files, so I tried to get it with a command
I tried to explain how to get the article content with MediaWiki API in an easy-to-understand manner with examples (Python 3)