Repository: line/line-bot-sdk-python
Pull request #301 (2020-10-14 05:47:58)

Support get bot info
Implemented following feature mentioned in #298 .

- Get bot info

Willing to receive any suggestion. If you please, label this PR as `hacktoberfest-accepted` , thanks.

louis70109: LGTM, 
@cryeo -san and @okue -san please check this.
CA-Lee: > Can you add get_bot_info() in document?

Sorry, I'm not clear about the documentation structure. Do I only need to modify the README.rst?
louis70109: > Sorry, I'm not clear about the documentation structure. Do I only need to modify the README.rst?

Yes~

Patch:
diff --git a/README.rst b/README.rst
index 368f99a..eff72b9 100644
--- a/README.rst
+++ b/README.rst
@@ -590,6 +590,25 @@ https://developers.line.biz/en/reference/messaging-api/#get-message-event
     broadcast_response = line_bot_api.broadcast(TextSendMessage(text='Hello World!'))
     insight = line_bot_api.get_insight_message_event(broadcast_response.request_id)
     print(insight.overview)
+    
+get\_bot_info(self, timeout=None)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Get bot's basic information.
+
+https://developers.line.biz/en/reference/messaging-api/#get-bot-info
+
+.. code:: python
+
+    bot_info = line_bot_api.get_bot_info()
+
+    print(bot_info.display_name)
+    print(bot_info.user_id)
+    print(bot_info.basic_id)
+    print(bot_info.premium_id)
+    print(bot_info.picture_url)
+    print(bot_info.chat_mode)
+    print(bot_info.mark_as_read_mode)
 
 ※ Error handling
 ^^^^^^^^^^^^^^^^^
diff --git a/linebot/api.py b/linebot/api.py
index 85bee50..7edb8d9 100644
--- a/linebot/api.py
+++ b/linebot/api.py
@@ -28,7 +28,7 @@ from .models import (
     MessageDeliveryPushResponse, MessageDeliveryReplyResponse,
     InsightMessageDeliveryResponse, InsightFollowersResponse, InsightDemographicResponse,
     InsightMessageEventResponse, BroadcastResponse, NarrowcastResponse,
-    MessageProgressNarrowcastResponse,
+    MessageProgressNarrowcastResponse, BotInfo,
 )
 from .models.responses import Group
 
@@ -1129,6 +1129,24 @@ class LineBotApi(object):
 
         return InsightMessageEventResponse.new_from_json_dict(response.json)
 
+    def get_bot_info(self, timeout=None):
+        """Get a bot's basic information.
+
+        https://developers.line.biz/en/reference/messaging-api/#get-bot-info
+        :param timeout: (optional) How long to wait for the server
+            to send data before giving up, as a float,
+            or a (connect timeout, read timeout) float tuple.
+            Default is self.http_client.timeout
+        :type timeout: float | tuple(float, float)
+        :rtype: :py:class:`linebot.models.responses.BotInfo`
+        """
+        response = self._get(
+            '/v2/bot/info',
+            timeout=timeout
+        )
+
+        return BotInfo.new_from_json_dict(response.json)
+
     def _get(self, path, endpoint=None, params=None, headers=None, stream=False, timeout=None):
         url = (endpoint or self.endpoint) + path
 
diff --git a/linebot/models/__init__.py b/linebot/models/__init__.py
index f321a37..0cac7fb 100644
--- a/linebot/models/__init__.py
+++ b/linebot/models/__init__.py
@@ -150,6 +150,7 @@ from .responses import (  # noqa
     BroadcastResponse,
     NarrowcastResponse,
     MessageProgressNarrowcastResponse,
+    BotInfo,
 )
 from .rich_menu import (  # noqa
     RichMenu,
diff --git a/linebot/models/responses.py b/linebot/models/responses.py
index 936c60a..aa4613a 100644
--- a/linebot/models/responses.py
+++ b/linebot/models/responses.py
@@ -496,3 +496,27 @@ class NarrowcastResponse(Base):
         super(NarrowcastResponse, self).__init__(**kwargs)
 
         self.request_id = request_id
+
+
+class BotInfo(Base):
+    """Response of `linebot.get_bot_info()` .
+
+    https://developers.line.biz/en/reference/messaging-api/#get-bot-info
+    """
+
+    def __init__(self, user_id=None, basic_id=None, premium_id=None,
+                 display_name=None, picture_url=None, chat_mode=None,
+                 mark_as_read_mode=None, **kwargs):
+        """__init__ method.
+
+        :param kwargs:
+        """
+        super(BotInfo, self).__init__(**kwargs)
+
+        self.user_id = user_id
+        self.basic_id = basic_id
+        self.premium_id = premium_id
+        self.display_name = display_name
+        self.picture_url = picture_url
+        self.chat_mode = chat_mode
+        self.mark_as_read_mode = mark_as_read_mode


Test patch:
diff --git a/tests/api/test_get_bot_info.py b/tests/api/test_get_bot_info.py
new file mode 100644
index 0000000..b1f9c89
--- /dev/null
+++ b/tests/api/test_get_bot_info.py
@@ -0,0 +1,64 @@
+# -*- coding: utf-8 -*-
+
+#  Licensed under the Apache License, Version 2.0 (the "License"); you may
+#  not use this file except in compliance with the License. You may obtain
+#  a copy of the License at
+#
+#       https://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#  License for the specific language governing permissions and limitations
+#  under the License.
+
+from __future__ import unicode_literals, absolute_import
+
+import unittest
+
+import responses
+
+from linebot import (
+    LineBotApi
+)
+
+
+class TestLineBotApi(unittest.TestCase):
+    def setUp(self):
+        self.tested = LineBotApi('channel_secret')
+
+    @responses.activate
+    def test_get_bot_info(self):
+        responses.add(
+            responses.GET,
+            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/info',
+            json={
+                "userId": "Ub9952f8...",
+                "basicId": "@216ru...",
+                "premiumId": "@example",
+                "displayName": "Example name",
+                "pictureUrl": "https://obs.line-apps.com/...",
+                "chatMode": "chat",
+                "markAsReadMode": "manual",
+            },
+            status=200
+        )
+
+        bot = self.tested.get_bot_info()
+
+        request = responses.calls[0].request
+        self.assertEqual(request.method, 'GET')
+        self.assertEqual(
+            request.url,
+            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/info')
+        self.assertEqual(bot.user_id, 'Ub9952f8...')
+        self.assertEqual(bot.basic_id, '@216ru...')
+        self.assertEqual(bot.premium_id, '@example')
+        self.assertEqual(bot.display_name, 'Example name')
+        self.assertEqual(bot.picture_url, 'https://obs.line-apps.com/...')
+        self.assertEqual(bot.chat_mode, 'chat')
+        self.assertEqual(bot.mark_as_read_mode, 'manual')
+
+
+if __name__ == '__main__':
+    unittest.main()