Skip to content

Commit

Permalink
fix for - mp2
Browse files Browse the repository at this point in the history
  • Loading branch information
spandan.mondal committed Aug 7, 2024
1 parent e4275dd commit 563042a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 4 additions & 0 deletions kairon/chat/handlers/channels/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ async def send_message_to_user(self, message: Any, recipient_id: str):
access_token = self.__get_access_token()
c = client(access_token, from_phone_number_id=phone_number_id)
message_type = "text"
if isinstance(message, str):
message = {
'body': message,
}
if isinstance(message, dict):
if message.get("type") == "image":
message_type = "image"
Expand Down
5 changes: 3 additions & 2 deletions kairon/shared/callback/data_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def check_nonempty_string(value, msg="Value must be a non-empty string"):
def encrypt_secret(secret: str) -> str:
secret = secret.encode("utf-8")
fernet = Fernet(Utility.environment['security']['fernet_key'].encode("utf-8"))
return fernet.encrypt(secret).decode("utf-8")
sec = fernet.encrypt(secret).decode("utf-8")
return sec[:24]


def decrypt_secret(secret: str) -> str:
Expand Down Expand Up @@ -58,7 +59,7 @@ def get_entry(bot, name) -> dict:
raise AppException(f"Callback Configuration with name '{name}' does not exist!")
dict_form = entry.to_mongo().to_dict()
dict_form.pop("_id")
return dict_form
return dict_form

@staticmethod
def create_entry(bot: str,
Expand Down
12 changes: 5 additions & 7 deletions tests/unit_test/callback_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ def test_check_nonempty_string():
check_nonempty_string("valid_string")


def test_encrypt_decrypt_secret():
def test_encrypt_secret():
secret = "my_secret"
encrypted = encrypt_secret(secret)
decrypted = decrypt_secret(encrypted)
assert secret == decrypted

assert len(encrypted) == 24

@patch("kairon.shared.callback.data_objects.CallbackConfig.save", MagicMock())
def test_create_callback_config():
Expand Down Expand Up @@ -283,13 +281,13 @@ async def test_send_message_to_user(mock_send):
message = 'Hello, World!'
recipient_id = 'user1'
await whatsapp.send_message_to_user(message, recipient_id)
mock_send.assert_called_once_with('Hello, World!', 'user1', 'text')
mock_send.assert_called_once_with({'body': 'Hello, World!'}, 'user1', 'text')
message = {'type': 'image', 'url': 'http://example.com/image.jpg'}
await whatsapp.send_message_to_user(message, recipient_id)
mock_send.assert_called_with({'type': 'image', 'url': 'http://example.com/image.jpg'}, 'user1', 'image')
mock_send.assert_called_with({'url': 'http://example.com/image.jpg'}, 'user1', 'image')
message = {'type': 'video', 'url': 'http://example.com/video.mp4'}
await whatsapp.send_message_to_user(message, recipient_id)
mock_send.assert_called_with({'type': 'video', 'url': 'http://example.com/video.mp4'}, 'user1', 'video')
mock_send.assert_called_with({'url': 'http://example.com/video.mp4'}, 'user1', 'video')


from kairon.async_callback.processor import CallbackProcessor
Expand Down

0 comments on commit 563042a

Please sign in to comment.