Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic_answers attribute processing for Integreat chat #3362

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions integreat_cms/api/v3/chat/user_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

from ....cms.models import ABTester, AttachmentMap, Language, Region, UserChat
from ...decorators import json_response
from .utils.chat_bot import process_answer, process_user_message
from .utils.chat_bot import (
process_translate_answer,
process_translate_question,
process_user_message,
)
from .utils.zammad_api import ZammadChatAPI

if TYPE_CHECKING:
Expand Down Expand Up @@ -220,6 +224,16 @@ def chat(
return send_message(request, language_slug, client, user_chat, device_id)


def is_app_user_message(webhook_message: dict) -> bool:
"""
Check if the message was sent by the Integreat App
"""
return (
webhook_message["article"]["created_by"]["login"]
== "[email protected]"
)


@csrf_exempt
@json_response
def zammad_webhook(request: HttpRequest) -> JsonResponse:
Expand All @@ -243,16 +257,21 @@ def zammad_webhook(request: HttpRequest) -> JsonResponse:
}
)
if (
webhook_message["article"]["created_by"]["login"]
== "[email protected]"
is_app_user_message(webhook_message)
and not webhook_message["ticket"]["automatic_answers"]
):
actions.append("question translation queued")
process_translate_question.apply_async(
args=[message_text, region.slug, webhook_message["ticket"]["id"]]
)
elif is_app_user_message(webhook_message):
actions.append("question translation and answering queued")
process_user_message.apply_async(
args=[message_text, region.slug, webhook_message["ticket"]["id"]]
)
else:
actions.append("answer translation queued")
process_answer.apply_async(
process_translate_answer.apply_async(
args=[message_text, region.slug, webhook_message["ticket"]["id"]]
)
return JsonResponse(
Expand Down
35 changes: 31 additions & 4 deletions integreat_cms/api/v3/chat/utils/chat_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ def process_user_message(
answer["answer"],
False,
True,
answer["automatic_answers"],
)


async def async_process_answer(
async def async_process_translate(
message_text: str, source_language: str, target_language: str
) -> dict:
"""
Expand All @@ -123,15 +124,17 @@ async def async_process_answer(


@shared_task
def process_answer(message_text: str, region_slug: str, zammad_ticket_id: int) -> None:
def process_translate_answer(
message_text: str, region_slug: str, zammad_ticket_id: int
) -> None:
"""
Process automatic or counselor answers
Process translation of automatic or counselor answers
"""
region = Region.objects.get(slug=region_slug)
zammad_chat = UserChat.objects.get(zammad_id=zammad_ticket_id, region=region)
client = ZammadChatAPI(region)
translation = asyncio.run(
async_process_answer(
async_process_translate(
message_text, region.default_language.slug, zammad_chat.language.slug
)
)
Expand All @@ -142,3 +145,27 @@ def process_answer(message_text: str, region_slug: str, zammad_ticket_id: int) -
False,
True,
)


@shared_task
def process_translate_question(
message_text: str, region_slug: str, zammad_ticket_id: int
) -> None:
"""
Process translation of app user questions
"""
region = Region.objects.get(slug=region_slug)
zammad_chat = UserChat.objects.get(zammad_id=zammad_ticket_id, region=region)
client = ZammadChatAPI(region)
translation = asyncio.run(
async_process_translate(
message_text, zammad_chat.language.slug, region.default_language.slug
)
)
if translation:
client.send_message(
zammad_chat.zammad_id,
translation["translation"],
True,
True,
)
3 changes: 3 additions & 0 deletions integreat_cms/api/v3/chat/utils/zammad_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def send_message(
message: str,
internal: bool = False,
automatic_message: bool = False,
automatic_answers: bool = True,
) -> dict:
# pylint: disable=method-hidden
"""
Expand All @@ -183,6 +184,7 @@ def send_message(
param message: The message body
param internal: keep the message internal in Zammad (do not show to user)
param automatic_message: sets title to "automatically generated message"
param automatic_answers: sets the attribute
return: dict with Zammad article data
"""
params = {
Expand All @@ -197,6 +199,7 @@ def send_message(
else "app user message"
),
"sender": "Customer" if not automatic_message else "Agent",
"automatic_answers": automatic_answers,
}
return self._parse_response( # type: ignore[return-value]
self._attempt_call(self.client.ticket_article.create, params=params)
Expand Down
Loading