在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:line/line-bot-sdk-python开源软件地址:https://github.com/line/line-bot-sdk-python开源编程语言:Python 100.0%开源软件介绍:LINE Messaging API SDK for PythonSDK of the LINE Messaging API for Python. IntroductionThe LINE Messaging API SDK for Python makes it easy to develop bots using LINE Messaging API, and you can create a sample bot within minutes. DocumentationSee the official API documentation for more information English: https://developers.line.biz/en/docs/messaging-api/overview/ Japanese: https://developers.line.biz/ja/docs/messaging-api/overview/ Requirements
Installation$ pip install line-bot-sdk SynopsisUsage: from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
app = Flask(__name__)
line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
handler = WebhookHandler('YOUR_CHANNEL_SECRET')
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
print("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=event.message.text))
if __name__ == "__main__":
app.run() APILineBotApihttps://api.line.me', timeout=5, http_client=RequestsHttpClient)__init__(self, channel_access_token, endpoint='Create a new LineBotApi instance. line_bot_api = linebot.LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN') You can override the reply_message(self, reply_token, messages, notification_disabled=False, timeout=None)Respond to events from users, groups, and rooms. You can get a reply_token from a webhook event object. https://developers.line.biz/en/reference/messaging-api/#send-reply-message line_bot_api.reply_message(reply_token, TextSendMessage(text='Hello World!')) push_message(self, to, messages, notification_disabled=False, custom_aggregation_units=None, timeout=None)Send messages to users, groups, and rooms at any time. https://developers.line.biz/en/reference/messaging-api/#send-push-message line_bot_api.push_message(to, TextSendMessage(text='Hello World!')) multicast(self, to, messages, notification_disabled=False, custom_aggregation_units=None, timeout=None)Send push messages to multiple users at any time. Messages cannot be sent to groups or rooms. https://developers.line.biz/en/reference/messaging-api/#send-multicast-message line_bot_api.multicast(['to1', 'to2'], TextSendMessage(text='Hello World!')) broadcast(self, messages, notification_disabled=False, timeout=None)Send push messages to multiple users at any time. https://developers.line.biz/en/reference/messaging-api/#send-broadcast-message line_bot_api.broadcast(TextSendMessage(text='Hello World!')) narrowcast(self, messages, recipient=None, filter=None, limit=None, timeout=None)Sends a push message to multiple users specified by attributes (such as age, gender, OS, and region) or retargeting (audiences). https://developers.line.biz/en/reference/messaging-api/#send-narrowcast-message line_bot_api.narrowcast(
messages=TextSendMessage(text='Hello World!'),
recipient=AudienceRecipient(group_id=5614991017776),
filter=Filter(demographic=AgeFilter(gte="age_35", lt="age_40")),
limit=Limit(max=10)
) get_progress_status_narrowcast(self, request_id, timeout=None)Get progress status of narrowcast messages sent. https://developers.line.biz/en/reference/messaging-api/#get-narrowcast-progress-status narrowcast_progress = line_bot_api.get_progress_status_narrowcast(request_id)
assert narrowcast_progress.phase == 'succeeded'
print(narrowcast.success_count)
print(narrowcast.failure_count)
print(narrowcast.target_count) get_profile(self, user_id, timeout=None)Get user profile information. https://developers.line.biz/en/reference/messaging-api/#get-profile profile = line_bot_api.get_profile(user_id)
print(profile.display_name)
print(profile.user_id)
print(profile.picture_url)
print(profile.status_message) get_group_summary(self, group_id, timeout=None)Gets the group ID, group name, and group icon URL of a group where the LINE Official Account is a member. https://developers.line.biz/en/reference/messaging-api/#get-group-summary summary = line_bot_api.get_group_summary(group_id)
print(summary.group_id)
print(summary.group_name)
print(summary.picture_url) get_group_member_profile(self, group_id, user_id, timeout=None)Gets the user profile of a member of a group that the bot is in. This can be the user ID of a user who has not added the bot as a friend or has blocked the bot. https://developers.line.biz/en/reference/messaging-api/#get-group-member-profile profile = line_bot_api.get_group_member_profile(group_id, user_id)
print(profile.display_name)
print(profile.user_id)
print(profile.picture_url) get_room_member_profile(self, room_id, user_id, timeout=None)Gets the user profile of a member of a room that the bot is in. This can be the user ID of a user who has not added the bot as a friend or has blocked the bot. https://developers.line.biz/en/reference/messaging-api/#get-room-member-profile profile = line_bot_api.get_room_member_profile(room_id, user_id)
print(profile.display_name)
print(profile.user_id)
print(profile.picture_url) get_group_member_ids(self, group_id, start=None, timeout=None)Gets the user IDs of the members of a group that the bot is in. This includes the user IDs of users who have not added the bot as a friend or has blocked the bot. https://developers.line.biz/en/reference/messaging-api/#get-group-member-user-ids member_ids_res = line_bot_api.get_group_member_ids(group_id)
print(member_ids_res.member_ids)
print(member_ids_res.next) get_room_member_ids(self, room_id, start=None, timeout=None)Gets the user IDs of the members of a room that the bot is in. This includes the user IDs of users who have not added the bot as a friend or has blocked the bot. https://developers.line.biz/en/reference/messaging-api/#get-room-member-user-ids member_ids_res = line_bot_api.get_room_member_ids(room_id)
print(member_ids_res.member_ids)
print(member_ids_res.next) get_group_members_count(self, group_id, timeout=None)Gets the count of members in a group. https://developers.line.biz/en/reference/messaging-api/#get-members-group-count group_count = line_bot_api.get_group_members_count(group_id)
print(group_count) get_room_members_count(self, room_id, timeout=None)Gets the count of members in a room. https://developers.line.biz/en/reference/messaging-api/#get-members-room-count room_count = line_bot_api.get_room_members_count(room_id)
print(room_count) get_message_content(self, message_id, timeout=None)Retrieve image, video, and audio data sent by users. https://developers.line.biz/en/reference/messaging-api/#get-content message_content = line_bot_api.get_message_content(message_id)
with open(file_path, 'wb') as fd:
for chunk in message_content.iter_content():
fd.write(chunk) leave_group(self, group_id, timeout=None)Leave a group. https://developers.line.biz/en/reference/messaging-api/#leave-group line_bot_api.leave_group(group_id) leave_room(self, room_id, timeout=None)Leave a room. https://developers.line.biz/en/reference/messaging-api/#leave-room line_bot_api.leave_room(room_id) get_rich_menu(self, rich_menu_id, timeout=None)Gets a rich menu via a rich menu ID. https://developers.line.biz/en/reference/messaging-api/#get-rich-menu rich_menu = line_bot_api.get_rich_menu(rich_menu_id)
print(rich_menu.rich_menu_id) create_rich_menu(self, rich_menu, timeout=None)Creates a rich menu. You must upload a rich menu image and link the rich menu to a user for the rich menu to be displayed. You can create up to 1000 rich menus for one LINE Official Account with the Messaging API. https://developers.line.biz/en/reference/messaging-api/#create-rich-menu rich_menu_to_create = RichMenu(
size=RichMenuSize(width=2500, height=843),
selected=False,
name="Nice richmenu",
chat_bar_text="Tap here",
areas=[RichMenuArea(
bounds=RichMenuBounds(x=0, y=0, width=2500, height=843),
action=URIAction(label='Go to line.me', uri='https://line.me'))]
)
rich_menu_id = line_bot_api.create_rich_menu(rich_menu=rich_menu_to_create)
print(rich_menu_id) delete_rich_menu(self, rich_menu_id, timeout=None)Deletes a rich menu. https://developers.line.biz/en/reference/messaging-api/#delete-rich-menu line_bot_api.delete_rich_menu(rich_menu_id) get_rich_menu_id_of_user(self, user_id, timeout=None)Gets the ID of the rich menu linked to a user. https://developers.line.biz/en/reference/messaging-api/#get-rich-menu-id-of-user rich_menu_id = line_bot_api.get_rich_menu_id_of_user(user_id)
print(rich_menu_id) link_rich_menu_to_user(self, user_id, rich_menu_id, timeout=None)Links a rich menu to a user. Only one rich menu can be linked to a user at one time. https://developers.line.biz/en/reference/messaging-api/#link-rich-menu-to-user line_bot_api.link_rich_menu_to_user(user_id, rich_menu_id) link_rich_menu_to_users(self, user_ids, rich_menu_id, timeout=None)Links a rich menu to multiple users. https://developers.line.biz/en/reference/messaging-api/#link-rich-menu-to-users line_bot_api.link_rich_menu_to_users(<user_ids>, <rich_menu_id>) unlink_rich_menu_from_user(self, user_id, timeout=None)Unlinks a rich menu from a user. https://developers.line.biz/en/reference/messaging-api/#unlink-rich-menu-from-user line_bot_api.unlink_rich_menu_from_user(user_id) unlink_rich_menu_from_users(self, user_ids, timeout=None)Unlinks rich menus from multiple users. https://developers.line.biz/en/reference/messaging-api/#unlink-rich-menu-from-users line_bot_api.unlink_rich_menu_from_users(<user_ids>) get_rich_menu_image(self, rich_menu_id, timeout=None)Downloads an image associated with a rich menu. https://developers.line.biz/en/reference/messaging-api/#download-rich-menu-image content = line_bot_api.get_rich_menu_image(rich_menu_id)
with open(file_path, 'wb') as fd:
for chunk in content.iter_content():
fd.write(chunk) set_rich_menu_image(self, rich_menu_id, content_type, content, timeout=None)Uploads and attaches an image to a rich menu. https://developers.line.biz/en/reference/messaging-api/#upload-rich-menu-image with open(file_path, 'rb') as f:
line_bot_api.set_rich_menu_image(rich_menu_id, content_type, f) get_rich_menu_list(self, timeout=None)Gets a list of all uploaded rich menus. https://developers.line.biz/en/reference/messaging-api/#get-rich-menu-list rich_menu_list = line_bot_api.get_rich_menu_list()
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论