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.
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.
[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)
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.
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.
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()
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.
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()
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
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()
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()
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