Exemple #1
0
class TestLineBotApi(unittest.TestCase):
    def setUp(self):
        self.tested = LineBotApi('channel_secret')

    @responses.activate
    def test_get_group_summary(self):
        responses.add(responses.GET,
                      LineBotApi.DEFAULT_API_ENDPOINT +
                      '/v2/bot/group/group_id/summary',
                      json={
                          "groupId": "Ca56f94637c...",
                          "groupName": "Group name",
                          "pictureUrl": "https://example.com/abcdefghijklmn"
                      },
                      status=200)

        group = self.tested.get_group_summary('group_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/group/group_id/summary')
        self.assertEqual(group.group_id, 'Ca56f94637c...')
        self.assertEqual(group.group_name, 'Group name')
        self.assertEqual(group.picture_url,
                         'https://example.com/abcdefghijklmn')

    @responses.activate
    def test_get_group_members_count(self):
        responses.add(responses.GET,
                      LineBotApi.DEFAULT_API_ENDPOINT +
                      '/v2/bot/group/group_id/members/count',
                      json={"count": 3},
                      status=200)

        count = self.tested.get_group_members_count('group_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url, LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/group/group_id/members/count')
        self.assertEqual(count, 3)
Exemple #2
0
    def message_event(event):
        line_bot_api = LineBotApi(os.getenv('LINE_CHANNEL_ACCESS_TOKEN'))

        line_type = event.source.type
        group, room, user = None, None, None
        if line_type == 'group':
            group = event.source.group_id
        elif line_type == 'room':
            room = event.source.room_id
        user = event.source.user_id

        token = event.reply_token
        message = event.message.text

        if message == '你走吧':
            msg = '走了88'
            if group:
                line_bot_api.reply_message(token, TextSendMessage(text=msg))
                line_bot_api.leave_group(group_id=group)
            elif room:
                line_bot_api.reply_message(token, TextSendMessage(text=msg))
                line_bot_api.leave_room(room_id=room)
            else:
                msg = '為什麼不是你走?'
                line_bot_api.reply_message(token, TextSendMessage(text=msg))
        elif group:
            if message == '群組資訊':
                count = line_bot_api.get_group_members_count(group_id=group)
                summary = line_bot_api.get_group_summary(group_id=group)
                text = f'群組名稱➡️ {summary.group_name}\n當前群組人數為➡️ {count}'
                line_bot_api.reply_message(
                    token,
                    messages=[
                        TextSendMessage(text=text,
                                        sender=Sender(
                                            name='CONY',
                                            icon_url=LINE_FRIEND['CONY'])),
                        ImageSendMessage(
                            original_content_url=summary.picture_url,
                            preview_image_url=summary.picture_url,
                        )
                    ])
            elif message == '我是誰':
                profile = line_bot_api.get_group_member_profile(group_id=group,
                                                                user_id=user)
                text = f'你是➡️ {profile.display_name}\nID➡️ {profile.user_id}'
                line_bot_api.reply_message(
                    token,
                    messages=[
                        TextSendMessage(text=text,
                                        sender=Sender(
                                            name='BROWN',
                                            icon_url=LINE_FRIEND['BROWN'])),
                        ImageSendMessage(
                            original_content_url=profile.picture_url,
                            preview_image_url=profile.picture_url,
                        )
                    ])
            else:
                message = '請邀請我進群組喔\n指令為: \n1. 我是誰\n2.群組資訊\n3. 你走吧\n3. 輸入 v1 降版'

        elif room:
            if message == '聊天室資訊':
                count = line_bot_api.get_room_members_count(room_id=room)
                text = f'聊天室人數為: {count}'
                line_bot_api.reply_message(token, TextSendMessage(text=text))
            elif message == '我是誰':
                profile = line_bot_api.get_room_member_profile(room_id=room,
                                                               user_id=user)
                text = f'你是➡️ {profile.display_name}\nID➡️ {profile.user_id}'
                line_bot_api.reply_message(
                    token,
                    messages=[
                        TextSendMessage(text=text),
                        ImageSendMessage(
                            original_content_url=profile.picture_url,
                            preview_image_url=profile.picture_url,
                        )
                    ])
        else:
            message = '請邀請我進群組喔\n指令為: \n1. 我是誰\n2.群組資訊\n3. 你走吧\n3. 輸入 v1 降版'
            if message == 'video':
                line_bot_api.reply_message(
                    token,
                    messages=VideoSendMessage(
                        original_content_url='https://i.imgur.com/BhBshUO.mp4',
                        preview_image_url='https://i.imgur.com/MW0Mpb6.jpg',
                        tracking_id='duck'))
            elif message == 'v1':
                result = line_bot_api.set_webhook_endpoint(
                    webhook_endpoint=
                    f"{os.getenv('MY_DOMAIN')}/v1/webhooks/line")
                if result == {}:
                    message = '降版!'
            else:
                message = '請邀請我進群組喔\n指令為: \n1. 我是誰\n2.群組資訊\n3. 你走吧\n3. 輸入 v1 降版'
        line_bot_api.reply_message(
            token,
            TextSendMessage(text=message,
                            sender=Sender(name='SALLY',
                                          icon_url=LINE_FRIEND['SALLY'])))

        response = {"statusCode": 200, "body": json.dumps({"message": 'ok'})}

        return response
Exemple #3
0
    def post(self):
        payload = request.get_json(force=True)

        line_bot_api = LineBotApi(os.getenv('LINE_CHANNEL_ACCESS_TOKEN'))
        handler = WebhookHandler(os.getenv('LINE_CHANNEL_SECRET'))
        event = payload['events'][0]
        token = event['replyToken']
        message = event['message']['text']
        line_type = event['source'].get('type')
        group = event['source'].get('groupId')
        room = event['source'].get('roomId')
        user = event['source'].get('userId')

        if message == '你走吧':
            if line_type == 'group':
                line_bot_api.reply_message(token, TextSendMessage(text='走了88'))
                line_bot_api.leave_group(group_id=group)
            elif line_type == 'room':
                line_bot_api.reply_message(token, TextSendMessage(text='走了88'))
                line_bot_api.leave_room(room_id=room)
            else:
                line_bot_api.reply_message(token,
                                           TextSendMessage(text='為什麼不是你走?'))

        elif line_type == 'group':
            if message == '群組資訊':
                count = line_bot_api.get_group_members_count(group_id=group)
                summary = line_bot_api.get_group_summary(group_id=group)
                text = f'群組名稱➡️ {summary.group_name}\n當前群組人數為➡️ {count}'
                line_bot_api.reply_message(
                    token,
                    messages=[
                        TextSendMessage(text=text),
                        ImageSendMessage(
                            original_content_url=summary.picture_url,
                            preview_image_url=summary.picture_url,
                        )
                    ])
            elif message == '我是誰':
                profile = line_bot_api.get_group_member_profile(group_id=group,
                                                                user_id=user)
                text = f'你是➡️ {profile.display_name}\nID➡️ {profile.user_id}'
                line_bot_api.reply_message(
                    token,
                    messages=[
                        TextSendMessage(text=text),
                        ImageSendMessage(
                            original_content_url=profile.picture_url,
                            preview_image_url=profile.picture_url,
                        )
                    ])

            line_bot_api.reply_message(token, TextSendMessage(text=message))

        elif line_type == 'room':
            if message == '聊天室資訊':
                count = line_bot_api.get_room_members_count(room_id=room)
                text = f'聊天室人數為: {count}'
                line_bot_api.reply_message(token, TextSendMessage(text=text))
            elif message == '我是誰':
                profile = line_bot_api.get_room_member_profile(room_id=room,
                                                               user_id=user)
                text = f'你是➡️ {profile.display_name}\nID➡️ {profile.user_id}'
                line_bot_api.reply_message(
                    token,
                    messages=[
                        TextSendMessage(text=text),
                        ImageSendMessage(
                            original_content_url=profile.picture_url,
                            preview_image_url=profile.picture_url,
                        )
                    ])

            line_bot_api.reply_message(token, TextSendMessage(text=message))

        else:
            line_bot_api.reply_message(token, TextSendMessage(text=message))

        response = {"statusCode": 200, "body": json.dumps({"message": 'ok'})}

        return response