Skip to content

Commit bb2a8dd

Browse files
authored
Support get bot info (#301)
* Add model: BotInfo * Add feature: get_bot_info * fix typo * Update feature: add test case of get_bot_info() * Update README.rst: add usage of get_bot_info()
1 parent 6805fde commit bb2a8dd

File tree

5 files changed

+127
-1
lines changed

5 files changed

+127
-1
lines changed

README.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,25 @@ https://developers.line.biz/en/reference/messaging-api/#get-message-event
590590
broadcast_response = line_bot_api.broadcast(TextSendMessage(text='Hello World!'))
591591
insight = line_bot_api.get_insight_message_event(broadcast_response.request_id)
592592
print(insight.overview)
593+
594+
get\_bot_info(self, timeout=None)
595+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
596+
597+
Get bot's basic information.
598+
599+
https://developers.line.biz/en/reference/messaging-api/#get-bot-info
600+
601+
.. code:: python
602+
603+
bot_info = line_bot_api.get_bot_info()
604+
605+
print(bot_info.display_name)
606+
print(bot_info.user_id)
607+
print(bot_info.basic_id)
608+
print(bot_info.premium_id)
609+
print(bot_info.picture_url)
610+
print(bot_info.chat_mode)
611+
print(bot_info.mark_as_read_mode)
593612
594613
※ Error handling
595614
^^^^^^^^^^^^^^^^^

linebot/api.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
MessageDeliveryPushResponse, MessageDeliveryReplyResponse,
2929
InsightMessageDeliveryResponse, InsightFollowersResponse, InsightDemographicResponse,
3030
InsightMessageEventResponse, BroadcastResponse, NarrowcastResponse,
31-
MessageProgressNarrowcastResponse,
31+
MessageProgressNarrowcastResponse, BotInfo,
3232
)
3333
from .models.responses import Group
3434

@@ -1129,6 +1129,24 @@ def get_insight_message_event(self, request_id, timeout=None):
11291129

11301130
return InsightMessageEventResponse.new_from_json_dict(response.json)
11311131

1132+
def get_bot_info(self, timeout=None):
1133+
"""Get a bot's basic information.
1134+
1135+
https://developers.line.biz/en/reference/messaging-api/#get-bot-info
1136+
:param timeout: (optional) How long to wait for the server
1137+
to send data before giving up, as a float,
1138+
or a (connect timeout, read timeout) float tuple.
1139+
Default is self.http_client.timeout
1140+
:type timeout: float | tuple(float, float)
1141+
:rtype: :py:class:`linebot.models.responses.BotInfo`
1142+
"""
1143+
response = self._get(
1144+
'/v2/bot/info',
1145+
timeout=timeout
1146+
)
1147+
1148+
return BotInfo.new_from_json_dict(response.json)
1149+
11321150
def _get(self, path, endpoint=None, params=None, headers=None, stream=False, timeout=None):
11331151
url = (endpoint or self.endpoint) + path
11341152

linebot/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
BroadcastResponse,
151151
NarrowcastResponse,
152152
MessageProgressNarrowcastResponse,
153+
BotInfo,
153154
)
154155
from .rich_menu import ( # noqa
155156
RichMenu,

linebot/models/responses.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,27 @@ def __init__(self, request_id=None, **kwargs):
496496
super(NarrowcastResponse, self).__init__(**kwargs)
497497

498498
self.request_id = request_id
499+
500+
501+
class BotInfo(Base):
502+
"""Response of `linebot.get_bot_info()` .
503+
504+
https://developers.line.biz/en/reference/messaging-api/#get-bot-info
505+
"""
506+
507+
def __init__(self, user_id=None, basic_id=None, premium_id=None,
508+
display_name=None, picture_url=None, chat_mode=None,
509+
mark_as_read_mode=None, **kwargs):
510+
"""__init__ method.
511+
512+
:param kwargs:
513+
"""
514+
super(BotInfo, self).__init__(**kwargs)
515+
516+
self.user_id = user_id
517+
self.basic_id = basic_id
518+
self.premium_id = premium_id
519+
self.display_name = display_name
520+
self.picture_url = picture_url
521+
self.chat_mode = chat_mode
522+
self.mark_as_read_mode = mark_as_read_mode

tests/api/test_get_bot_info.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
from __future__ import unicode_literals, absolute_import
16+
17+
import unittest
18+
19+
import responses
20+
21+
from linebot import (
22+
LineBotApi
23+
)
24+
25+
26+
class TestLineBotApi(unittest.TestCase):
27+
def setUp(self):
28+
self.tested = LineBotApi('channel_secret')
29+
30+
@responses.activate
31+
def test_get_bot_info(self):
32+
responses.add(
33+
responses.GET,
34+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/info',
35+
json={
36+
"userId": "Ub9952f8...",
37+
"basicId": "@216ru...",
38+
"premiumId": "@example",
39+
"displayName": "Example name",
40+
"pictureUrl": "https://obs.line-apps.com/...",
41+
"chatMode": "chat",
42+
"markAsReadMode": "manual",
43+
},
44+
status=200
45+
)
46+
47+
bot = self.tested.get_bot_info()
48+
49+
request = responses.calls[0].request
50+
self.assertEqual(request.method, 'GET')
51+
self.assertEqual(
52+
request.url,
53+
LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/info')
54+
self.assertEqual(bot.user_id, 'Ub9952f8...')
55+
self.assertEqual(bot.basic_id, '@216ru...')
56+
self.assertEqual(bot.premium_id, '@example')
57+
self.assertEqual(bot.display_name, 'Example name')
58+
self.assertEqual(bot.picture_url, 'https://obs.line-apps.com/...')
59+
self.assertEqual(bot.chat_mode, 'chat')
60+
self.assertEqual(bot.mark_as_read_mode, 'manual')
61+
62+
63+
if __name__ == '__main__':
64+
unittest.main()

0 commit comments

Comments
 (0)