Skip to content

Commit

Permalink
Merge pull request #14 from dfgh012316/support-sticker-message
Browse files Browse the repository at this point in the history
feat: Support sticker message and check status api in cli
  • Loading branch information
louis70109 authored Dec 25, 2023
2 parents 487c0f9 + ca71f8f commit 1bb83ee
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 6 deletions.
13 changes: 11 additions & 2 deletions lotify/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ def cli():

@cli.command()
@click.option('-t', '--access_token', type=str, help='LINE Notify access token', required=True)
@click.option('-m', '--message', type=str, help='send text message', required=True)
@click.option('-m', '--message', type=str, help='send text message')
@click.option('-u', '--image-url', type=str, help='Send image with image url', default=None)
@click.option('-f', '--image-file', type=str, help='Send image file with local file', default=None)
def send_message(message, access_token, image_url, image_file):
@click.option('--sticker-package-id', type=str, help='Sticker package id', default=None)
@click.option('--sticker-id', type=str, help='Sticker id', default=None)
@click.option('-c', '--check-status', is_flag=True, help='Check status', default=False)
def send_message(message, access_token, image_url, image_file,
sticker_package_id, sticker_id, check_status):
client = Client()
if check_status:
status = client.status(access_token)
click.echo("Status: {status}".format(status=status))
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'))
elif sticker_package_id is not None and sticker_id is not None:
client.send_message_with_sticker(access_token, message, sticker_id, sticker_package_id)
else:
client.send_message(access_token, message)
click.echo("Send notification success.")
8 changes: 4 additions & 4 deletions lotify/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def __init__(self,
bot_origin=None,
api_origin=None,
*args, **kwargs):
super(Client, self).__init__(*args, **kwargs)
self.client_id = client_id or self.CLIENT_ID
self.client_secret = client_secret or self.CLIENT_SECRET
self.redirect_uri = redirect_uri or self.REDIRECT_URI
Expand Down Expand Up @@ -86,8 +85,8 @@ def send_message_with_sticker(
notification_disabled=False):
params = {
'message': message,
'stickerId': sticker_id,
'stickerPackageId': sticker_package_id
'stickerPackageId': sticker_package_id,
'stickerId': sticker_id
}
if notification_disabled:
params.update({'notificationDisabled': notification_disabled})
Expand All @@ -96,7 +95,8 @@ def send_message_with_sticker(
url='{url}/api/notify'.format(url=self.api_origin),
data=params,
headers={
'Authorization': 'Bearer {token}'.format(token=access_token)
'Authorization': 'Bearer {token}'.format(token=access_token),
'Content-Type': 'application/x-www-form-urlencoded'
})
return response.json()

Expand Down
125 changes: 125 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import unittest
import responses
from unittest.mock import patch
from click.testing import CliRunner
from lotify.cli import send_message


class TestCli(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()
self.token = "YgVGZjHTprZdsooj5xOGXBl9vVwLGBEbjyjaBWY1Mbe"
self.bot_origin = "https://notify-bot.line.me"
self.api_origin = "https://notify-api.line.me"

@responses.activate
@patch("lotify.client.Client")
def test_check_status(self, mock_client):
responses.add(
responses.GET,
"{url}/api/status".format(url=self.api_origin),
json={"status": 200, "message": "ok"},
status=200,
)
mock_client.status.return_value = {"status": 200, "message": "ok"}
result = self.runner.invoke(
send_message, ["--check-status", "--access_token", self.token]
)
self.assertEqual(result.output, "Status: {'status': 200, 'message': 'ok'}\n")

@responses.activate
@patch("lotify.client.Client")
def test_send_message(self, mock_client):
responses.add(
responses.POST,
"{url}/api/notify".format(url=self.api_origin),
json={"status": 200, "message": "ok"},
status=200,
)
mock_client.send_message.return_value = {"status": 200, "message": "ok"}
result = self.runner.invoke(
send_message, ["--access_token", self.token, "--message", "Hello"]
)
self.assertEqual(result.output, "Send notification success.\n")

@responses.activate
@patch("lotify.client.Client")
def test_send_image_with_image_url(self, mock_client):
responses.add(
responses.POST,
"{url}/api/notify".format(url=self.api_origin),
json={"status": 200, "message": "ok"},
status=200,
)
mock_client.send_message_with_image_url.return_value = {
"status": 200,
"message": "ok",
}
result = self.runner.invoke(
send_message,
[
"--access_token",
self.token,
"--message",
"Hello",
"--image-url",
"https://example.com/image.png",
],
)
self.assertEqual(result.output, "Send notification success.\n")

@responses.activate
@patch("lotify.client.Client")
def test_send_image_with_image_file(self, mock_client):
responses.add(
responses.POST,
"{url}/api/notify".format(url=self.api_origin),
json={"status": 200, "message": "ok"},
status=200,
)

with self.runner.isolated_filesystem():
with open("image.png", "w") as f:
f.write("This is file object")
mock_client.send_message_with_image_file.return_value = {
"status": 200,
"message": "ok",
}
result = self.runner.invoke(
send_message,
[
"--access_token",
self.token,
"--message",
"Hello",
"--image-file",
"image.png",
],
)

self.assertEqual(result.output, "Send notification success.\n")

@responses.activate
@patch("lotify.client.Client")
def test_send_sticker_message(self, mock_client):
responses.add(
responses.POST,
"{url}/api/notify".format(url=self.api_origin),
json={"status": 200, "message": "ok"},
status=200,
)
mock_client.send_sticker_message.return_value = {"status": 200, "message": "ok"}
result = self.runner.invoke(
send_message,
[
"--access_token",
self.token,
"--message",
"Hello",
"--sticker-package-id",
"1",
"--sticker-id",
"1",
],
)
self.assertEqual(result.output, "Send notification success.\n")

0 comments on commit 1bb83ee

Please sign in to comment.