def send_message(message, access_token, image_url, image_file): client = Client() if image_url is not None: client.send_message_with_image_url(access_token, message, image_url, image_url) elif image_file is not None: client.send_message_with_image_file(access_token, message, open(image_file, 'rb')) else: client.send_message(access_token, message) click.echo("Send notification success.")
def send_message(message, access_token, image_url, image_file): client = Client() if image_url != "": client.send_message_with_image_url(access_token, message, image_url, image_url) elif image_file != "": client.send_message_with_image_file(access_token, message, open(image_file, 'rb')) else: client.send_message(access_token, message) click.echo("send successfully")
if __name__ == '__main__': lotify = Client() parser = argparse.ArgumentParser() parser.add_argument("--token", required=True, help="LINE Notify access_token") parser.add_argument("--message", required=True, help="LINE Notify message") parser.add_argument("--image-url", help="Image url") parser.add_argument("--image-file", help="Image file") parser.add_argument("--sticker-id", help="LINE sticker id") parser.add_argument("--package-id", help="LINE package id") args = parser.parse_args() if args.image_url: lotify.send_message_with_image_url(message=args.message, access_token=args.token, image_fullsize=args.image_url, image_thumbnail=args.image_url) elif args.sticker_id and args.package_id: lotify.send_message_with_sticker(message=args.message, access_token=args.token, sticker_id=args.sticker_id, sticker_package_id=args.package_id) elif args.image_file: lotify.send_message_with_image_file(message=args.message, access_token=args.token, file=open(args.image_file, 'rb').read()) else: lotify.send_message(message=args.message, access_token=args.token)
class TestClient(unittest.TestCase): def setUp(self): self.tested = Client( client_id='QxUxF..........i51eITH', client_secret='fmtJS3GOVTPn4....................bFcIvJf1jo', redirect_uri='http://localhost:5000/notify') self.token = '123456789abcdefghidhFeXkIQVjmuI6Oz123456789' self.bot_origin = "https://notify-bot.line.me" self.api_origin = "https://notify-api.line.me" def test_get_auth_link(self): result = self.tested.get_auth_link('foo') expected_query_string = { 'scope': 'notify', 'response_type': 'code', 'client_id': 'QxUxF..........i51eITH', 'redirect_uri': 'http://localhost:5000/notify', 'state': 'foo' } self.assertEqual( result, '{url}/oauth/authorize?{query_string}'.format( url=self.bot_origin, query_string=urlencode(expected_query_string))) @responses.activate def test_get_access_token(self): responses.add(responses.POST, '{url}/oauth/token'.format(url=self.bot_origin), json={'access_token': self.token}, status=200) result = self.tested.get_access_token('foo') request = responses.calls[0] response = json.loads(request.response.content.decode()) self.assertEqual('POST', request.request.method) self.assertEqual(self.token, response.get('access_token')) self.assertEqual(result, response.get('access_token')) @responses.activate def test_status(self): expect_response = { 'status': 200, 'message': 'ok', 'targetType': 'USER', 'target': 'NiJia Lin' } responses.add(responses.GET, '{url}/api/status'.format(url=self.api_origin), json=expect_response, status=200) result = self.tested.status(self.token) request = responses.calls[0] response = json.loads(request.response.content.decode()) self.assertEqual('GET', request.request.method) self.assertEqual(200, response.get('status')) self.assertEqual(result, expect_response) @responses.activate def test_send_message(self): expect_response = {'status': 200, 'message': 'ok'} responses.add(responses.POST, '{url}/api/notify'.format(url=self.api_origin), json=expect_response, status=200) result = self.tested.send_message(self.token, message='This is notify message') request = responses.calls[0] response = json.loads(request.response.content.decode()) self.assertEqual('POST', request.request.method) self.assertEqual(200, response.get('status')) self.assertEqual(result, expect_response) @responses.activate def test_send_message_with_sticker(self): expect_response = {'status': 200, 'message': 'ok'} responses.add(responses.POST, '{url}/api/notify'.format(url=self.api_origin), json=expect_response, status=200) result = self.tested.send_message_with_sticker( self.token, message='This is notify message', sticker_package_id=1, sticker_id=1) request = responses.calls[0] response = json.loads(request.response.content.decode()) self.assertEqual('POST', request.request.method) self.assertEqual(200, response.get('status')) self.assertEqual(result, expect_response) @responses.activate def test_send_message_with_image_url(self): expect_response = {'status': 200, 'message': 'ok'} responses.add(responses.POST, '{url}/api/notify'.format(url=self.api_origin), json=expect_response, status=200) result = self.tested.send_message_with_image_url( self.token, message='This is notify message', image_thumbnail='https://image.com/abc.png', image_fullsize='https://image.com/abc.png') request = responses.calls[0] response = json.loads(request.response.content.decode()) self.assertEqual('POST', request.request.method) self.assertEqual(200, response.get('status')) self.assertEqual(result, expect_response) @responses.activate def test_send_message_with_image_file(self): expect_response = {'status': 200, 'message': 'ok'} responses.add(responses.POST, '{url}/api/notify'.format(url=self.api_origin), json=expect_response, status=200) result = self.tested.send_message_with_image_file( self.token, message='This is notify message', file='This is file object') request = responses.calls[0] response = json.loads(request.response.content.decode()) self.assertEqual('POST', request.request.method) self.assertEqual(200, response.get('status')) self.assertEqual(result, expect_response) @responses.activate def test_revoke(self): expect_response = {'status': 200, 'message': 'ok'} responses.add(responses.POST, '{url}/api/revoke'.format(url=self.api_origin), json=expect_response, status=200) result = self.tested.revoke('access_token') request = responses.calls[0] response = json.loads(request.response.content.decode()) self.assertEqual('POST', request.request.method) self.assertEqual(200, response.get('status')) self.assertEqual(result, expect_response)