diff --git a/.DS_Store b/.DS_Store index be998a0d..f9c1890a 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index 6058a9b8..20b52e67 100644 Binary files a/osinaweb/db.sqlite3 and b/osinaweb/db.sqlite3 differ diff --git a/osinaweb/osichat/__pycache__/admin.cpython-310.pyc b/osinaweb/osichat/__pycache__/admin.cpython-310.pyc index ff08f7b4..6e0809cb 100644 Binary files a/osinaweb/osichat/__pycache__/admin.cpython-310.pyc and b/osinaweb/osichat/__pycache__/admin.cpython-310.pyc differ diff --git a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc index 9b2cb8d7..2bcaa00f 100644 Binary files a/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc and b/osinaweb/osichat/__pycache__/consumers.cpython-310.pyc differ diff --git a/osinaweb/osichat/__pycache__/models.cpython-310.pyc b/osinaweb/osichat/__pycache__/models.cpython-310.pyc index 52e4d923..2323309b 100644 Binary files a/osinaweb/osichat/__pycache__/models.cpython-310.pyc and b/osinaweb/osichat/__pycache__/models.cpython-310.pyc differ diff --git a/osinaweb/osichat/__pycache__/routing.cpython-310.pyc b/osinaweb/osichat/__pycache__/routing.cpython-310.pyc index a6a9cf3c..52d7d75f 100644 Binary files a/osinaweb/osichat/__pycache__/routing.cpython-310.pyc and b/osinaweb/osichat/__pycache__/routing.cpython-310.pyc differ diff --git a/osinaweb/osichat/admin.py b/osinaweb/osichat/admin.py index a615015f..44f8b1bf 100644 --- a/osinaweb/osichat/admin.py +++ b/osinaweb/osichat/admin.py @@ -3,9 +3,10 @@ from .models import * # Register your models here. admin.site.register(ChatRoom) +admin.site.register(ChatRoomGuest) admin.site.register(ChatMember) admin.site.register(ChatProject) admin.site.register(ChatMessage) admin.site.register(ChatMessageAttachment) admin.site.register(ChatMessageReaction) -admin.site.register(ChatMessageSeen) \ No newline at end of file +admin.site.register(ChatMessageSeen) diff --git a/osinaweb/osichat/consumers.py b/osinaweb/osichat/consumers.py index 30bf1bd5..f0c070e8 100644 --- a/osinaweb/osichat/consumers.py +++ b/osinaweb/osichat/consumers.py @@ -3,38 +3,113 @@ from .models import * import json from django.template.loader import render_to_string from asgiref.sync import async_to_sync +from django.shortcuts import get_object_or_404 class OsitcomChatRoom(WebsocketConsumer): def connect(self): + self.session_id = self.scope['url_route']['kwargs']['session_id'] async_to_sync(self.channel_layer.group_add)( - 'ositcom_chat', self.channel_name + self.session_id, self.channel_name ) self.accept() - self.send_template() + self.load_chat() def disconnect(self, close_code): async_to_sync(self.channel_layer.group_discard)( - "ositcom_chat", - self.channel_name + self.session_id, self.channel_name ) - def send_template(self): + + def load_chat(self): event = { - 'type': 'send_template_handler', + 'type': 'load_chat_handler', } async_to_sync(self.channel_layer.group_send)( - 'ositcom_chat', event + self.session_id, event ) - def send_template_handler(self, event): + def receive(self, text_data): + text_data_json = json.loads(text_data) + event_type = text_data_json.get('event_type') + + if event_type == 'start_conversation': + event = { + 'type': 'start_conversation_handler', + 'guest_name': text_data_json.get('guest_name'), + 'guest_mobile_number': text_data_json.get('guest_mobile_number'), + } + async_to_sync(self.channel_layer.group_send)( + self.session_id, event + ) + + if event_type == 'send_message': + event = { + 'type': 'send_message_handler', + 'body': text_data_json['message'] + } + async_to_sync(self.channel_layer.group_send)( + self.session_id, event + ) + + + + def load_chat_handler(self, event): + chat_room = None + chat_room_messages = None + chat_room_guest = ChatRoomGuest.objects.filter(session_id=self.session_id).last() + if chat_room_guest: + chat_room = chat_room_guest.room + self.chat_room = chat_room + chat_room_messages = ChatMessage.objects.filter(room=chat_room).order_by('date_sent') + context = { + 'chat_room': chat_room, + 'chat_room_messages': chat_room_messages, + } + html = render_to_string("chat-widget.html", context=context) + self.send(text_data=json.dumps({ + 'event_type': 'load_chat', + 'html': html, + })) + + + def start_conversation_handler(self, event): + chat_room = ChatRoom.objects.create( + name=f"Support: {self.session_id}", + date_created = datetime.now() + ) + chat_room_guest = ChatRoomGuest.objects.create( + room=chat_room, + name=event['guest_name'], + mobile_number= event['guest_mobile_number'], + session_id=self.session_id + ) context = { + 'chat_room': chat_room, + 'session_id':self.session_id + } + html = render_to_string("conversation.html", context=context) + self.send(text_data=json.dumps({ + 'event_type': 'start_conversation', + 'html': html, + })) + + def send_message_handler(self, event): + chat_message = ChatMessage.objects.create( + room = self.chat_room, + content=event['body'], + date_sent = datetime.now() + ) + context = { + 'chat_message': chat_message, } - html = render_to_string("test.html", context=context) + print('lokf') + html = render_to_string("partials/message.html", context=context) self.send(text_data=json.dumps({ - 'event_type': 'chat', + 'event_type': 'send_message', 'html': html, })) + diff --git a/osinaweb/osichat/migrations/0002_chatroom_guest_session_alter_chatroom_created_by_and_more.py b/osinaweb/osichat/migrations/0002_chatroom_guest_session_alter_chatroom_created_by_and_more.py new file mode 100644 index 00000000..8151bdcb --- /dev/null +++ b/osinaweb/osichat/migrations/0002_chatroom_guest_session_alter_chatroom_created_by_and_more.py @@ -0,0 +1,31 @@ +# Generated by Django 4.2.5 on 2024-07-20 08:19 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('osichat', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='chatroom', + name='guest_session', + field=models.CharField(blank=True, max_length=300, null=True), + ), + migrations.AlterField( + model_name='chatroom', + name='created_by', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL), + ), + migrations.AlterField( + model_name='chatroom', + name='name', + field=models.CharField(max_length=300), + ), + ] diff --git a/osinaweb/osichat/migrations/0003_chatroomguest_remove_chatroom_guest_session.py b/osinaweb/osichat/migrations/0003_chatroomguest_remove_chatroom_guest_session.py new file mode 100644 index 00000000..6c011c6f --- /dev/null +++ b/osinaweb/osichat/migrations/0003_chatroomguest_remove_chatroom_guest_session.py @@ -0,0 +1,26 @@ +# Generated by Django 4.2.5 on 2024-07-20 08:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('osichat', '0002_chatroom_guest_session_alter_chatroom_created_by_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='ChatRoomGuest', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=300)), + ('mobile_number', models.CharField(max_length=100)), + ('session_id', models.CharField(max_length=300)), + ], + ), + migrations.RemoveField( + model_name='chatroom', + name='guest_session', + ), + ] diff --git a/osinaweb/osichat/migrations/0004_chatroomguest_room.py b/osinaweb/osichat/migrations/0004_chatroomguest_room.py new file mode 100644 index 00000000..c8401fcf --- /dev/null +++ b/osinaweb/osichat/migrations/0004_chatroomguest_room.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.5 on 2024-07-20 08:29 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('osichat', '0003_chatroomguest_remove_chatroom_guest_session'), + ] + + operations = [ + migrations.AddField( + model_name='chatroomguest', + name='room', + field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, to='osichat.chatroom'), + ), + ] diff --git a/osinaweb/osichat/migrations/0005_alter_chatmessage_member.py b/osinaweb/osichat/migrations/0005_alter_chatmessage_member.py new file mode 100644 index 00000000..2b06a3a8 --- /dev/null +++ b/osinaweb/osichat/migrations/0005_alter_chatmessage_member.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.5 on 2024-07-20 12:36 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('osichat', '0004_chatroomguest_room'), + ] + + operations = [ + migrations.AlterField( + model_name='chatmessage', + name='member', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/osinaweb/osichat/migrations/0006_alter_chatmessage_content.py b/osinaweb/osichat/migrations/0006_alter_chatmessage_content.py new file mode 100644 index 00000000..bb152113 --- /dev/null +++ b/osinaweb/osichat/migrations/0006_alter_chatmessage_content.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.5 on 2024-07-20 12:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('osichat', '0005_alter_chatmessage_member'), + ] + + operations = [ + migrations.AlterField( + model_name='chatmessage', + name='content', + field=models.TextField(blank=True, null=True), + ), + ] diff --git a/osinaweb/osichat/migrations/0007_chatmessage_room.py b/osinaweb/osichat/migrations/0007_chatmessage_room.py new file mode 100644 index 00000000..0b87e7cc --- /dev/null +++ b/osinaweb/osichat/migrations/0007_chatmessage_room.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.5 on 2024-07-20 13:04 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('osichat', '0006_alter_chatmessage_content'), + ] + + operations = [ + migrations.AddField( + model_name='chatmessage', + name='room', + field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, to='osichat.chatroom'), + ), + ] diff --git a/osinaweb/osichat/migrations/0008_alter_chatmessage_room.py b/osinaweb/osichat/migrations/0008_alter_chatmessage_room.py new file mode 100644 index 00000000..bdf29a24 --- /dev/null +++ b/osinaweb/osichat/migrations/0008_alter_chatmessage_room.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.5 on 2024-07-20 13:44 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('osichat', '0007_chatmessage_room'), + ] + + operations = [ + migrations.AlterField( + model_name='chatmessage', + name='room', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='osichat.chatroom'), + ), + ] diff --git a/osinaweb/osichat/migrations/0009_visitor.py b/osinaweb/osichat/migrations/0009_visitor.py new file mode 100644 index 00000000..3d3703b5 --- /dev/null +++ b/osinaweb/osichat/migrations/0009_visitor.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.5 on 2024-07-21 16:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('osichat', '0008_alter_chatmessage_room'), + ] + + operations = [ + migrations.CreateModel( + name='Visitor', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('session_id', models.CharField(max_length=300)), + ('ip_address', models.CharField(max_length=300)), + ('url', models.URLField()), + ('reference', models.URLField()), + ], + ), + ] diff --git a/osinaweb/osichat/migrations/0010_visitor_left_date_visitor_visit_date.py b/osinaweb/osichat/migrations/0010_visitor_left_date_visitor_visit_date.py new file mode 100644 index 00000000..7bfd2f64 --- /dev/null +++ b/osinaweb/osichat/migrations/0010_visitor_left_date_visitor_visit_date.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.5 on 2024-07-21 16:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('osichat', '0009_visitor'), + ] + + operations = [ + migrations.AddField( + model_name='visitor', + name='left_date', + field=models.DateTimeField(null=True), + ), + migrations.AddField( + model_name='visitor', + name='visit_date', + field=models.DateTimeField(null=True), + ), + ] diff --git a/osinaweb/osichat/migrations/__pycache__/0002_chatroom_guest_session_alter_chatroom_created_by_and_more.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0002_chatroom_guest_session_alter_chatroom_created_by_and_more.cpython-310.pyc new file mode 100644 index 00000000..217651fc Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0002_chatroom_guest_session_alter_chatroom_created_by_and_more.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/__pycache__/0003_chatroomguest_remove_chatroom_guest_session.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0003_chatroomguest_remove_chatroom_guest_session.cpython-310.pyc new file mode 100644 index 00000000..053bd14b Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0003_chatroomguest_remove_chatroom_guest_session.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/__pycache__/0004_chatroomguest_room.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0004_chatroomguest_room.cpython-310.pyc new file mode 100644 index 00000000..867efa3a Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0004_chatroomguest_room.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/__pycache__/0005_alter_chatmessage_member.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0005_alter_chatmessage_member.cpython-310.pyc new file mode 100644 index 00000000..bb09fc97 Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0005_alter_chatmessage_member.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/__pycache__/0006_alter_chatmessage_content.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0006_alter_chatmessage_content.cpython-310.pyc new file mode 100644 index 00000000..7da4ebec Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0006_alter_chatmessage_content.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/__pycache__/0007_chatmessage_room.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0007_chatmessage_room.cpython-310.pyc new file mode 100644 index 00000000..50ca3731 Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0007_chatmessage_room.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/__pycache__/0008_alter_chatmessage_room.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0008_alter_chatmessage_room.cpython-310.pyc new file mode 100644 index 00000000..632f6a63 Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0008_alter_chatmessage_room.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/__pycache__/0009_visitor.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0009_visitor.cpython-310.pyc new file mode 100644 index 00000000..ddda825b Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0009_visitor.cpython-310.pyc differ diff --git a/osinaweb/osichat/migrations/__pycache__/0010_visitor_left_date_visitor_visit_date.cpython-310.pyc b/osinaweb/osichat/migrations/__pycache__/0010_visitor_left_date_visitor_visit_date.cpython-310.pyc new file mode 100644 index 00000000..6afc5c9d Binary files /dev/null and b/osinaweb/osichat/migrations/__pycache__/0010_visitor_left_date_visitor_visit_date.cpython-310.pyc differ diff --git a/osinaweb/osichat/models.py b/osinaweb/osichat/models.py index 80a5eab8..16b4eaf4 100644 --- a/osinaweb/osichat/models.py +++ b/osinaweb/osichat/models.py @@ -2,12 +2,28 @@ from django.db import models from osinacore.models import * # Create your models here. +class Visitor(models.Model): + session_id = models.CharField(max_length=300) + ip_address = models.CharField(max_length=300) + url = models.URLField() + reference = models.URLField() + visit_date = models.DateTimeField(null=True) + left_date = models.DateTimeField(null=True) + class ChatRoom(models.Model): - name = models.CharField(max_length=50) - created_by = models.ForeignKey(User, null=True, on_delete=models.SET_NULL) + name = models.CharField(max_length=300) + created_by = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, blank=True) date_created = models.DateTimeField() +class ChatRoomGuest(models.Model): + room = models.OneToOneField(ChatRoom, on_delete=models.CASCADE, null=True) + name = models.CharField(max_length=300) + mobile_number = models.CharField(max_length=100) + session_id = models.CharField(max_length=300) + + + class ChatMember(models.Model): member = models.ForeignKey(User, on_delete=models.CASCADE) room = models.ForeignKey(ChatRoom, on_delete=models.CASCADE) @@ -21,8 +37,9 @@ class ChatProject(models.Model): class ChatMessage(models.Model): - member = models.ForeignKey(User, null=True, on_delete=models.SET_NULL) - content = models.TimeField(null=True, blank=True) + room = models.ForeignKey(ChatRoom, on_delete=models.CASCADE, null=True) + member = models.ForeignKey(User, null=True, on_delete=models.SET_NULL, blank=True) + content = models.TextField(null=True, blank=True) date_sent = models.DateTimeField() diff --git a/osinaweb/osichat/routing.py b/osinaweb/osichat/routing.py index cb07f4a8..1d4d14d1 100644 --- a/osinaweb/osichat/routing.py +++ b/osinaweb/osichat/routing.py @@ -2,6 +2,6 @@ from django.urls import path from .consumers import * websocket_urlpatterns = [ - path("ws/osichat/", OsitcomChatRoom.as_asgi()), + path("ws/osichat//", OsitcomChatRoom.as_asgi()), ] \ No newline at end of file diff --git a/osinaweb/osichat/templates/chat-widget.html b/osinaweb/osichat/templates/chat-widget.html new file mode 100644 index 00000000..172ed99f --- /dev/null +++ b/osinaweb/osichat/templates/chat-widget.html @@ -0,0 +1,28 @@ +
+ {% if chat_room %} + {% include 'conversation.html' %} + {% else %} + {% include 'start-conversation.html' %} + {% endif %} +
+ + + +
+ + + + + +

Chat with us

+
+ + \ No newline at end of file diff --git a/osinaweb/osichat/templates/conversation.html b/osinaweb/osichat/templates/conversation.html new file mode 100644 index 00000000..aaf6cc0a --- /dev/null +++ b/osinaweb/osichat/templates/conversation.html @@ -0,0 +1,73 @@ +{%load static%} +
+
+
+ +
+
+
+ + + + + + + + + + + + + +
+
+
+

Hello {{chat_room.chatroomguest.name}}, the support department is notified about your request to start a conversation. It usually takes few minutes for you to receieve a reply.

+
+
+ +
+ {% for message in chat_room_messages %} +
+

{{message.content}}

+
+ {% endfor %} +
+ +
+ +
+ + +
+ + +
+
+ +
+ + + + + +

Chat with us

+
+ + + + diff --git a/osinaweb/osichat/templates/partials/message.html b/osinaweb/osichat/templates/partials/message.html new file mode 100644 index 00000000..5a52ee30 --- /dev/null +++ b/osinaweb/osichat/templates/partials/message.html @@ -0,0 +1,13 @@ +
+

{{chat_message.content}}

+
+ + diff --git a/osinaweb/osichat/templates/start-conversation.html b/osinaweb/osichat/templates/start-conversation.html new file mode 100644 index 00000000..07ffa6ef --- /dev/null +++ b/osinaweb/osichat/templates/start-conversation.html @@ -0,0 +1,66 @@ +{% load static %} +
+
+ +
+
+ +

Welcome to Ositcom

+
+ +
+

+ We are here 24/7/365 to help with sales or support queries. To start your conversation enter your + name + and mobile number below. +

+ +
+ + +
+
+
+ +
+
+

We are online

+ +

Typically replies in a few minutes

+ + +
+
+
+
+ + + +
+ + + + + +

Chat with us

+
+ + \ No newline at end of file diff --git a/osinaweb/osichat/templates/test.html b/osinaweb/osichat/templates/test.html deleted file mode 100644 index 28ab7384..00000000 --- a/osinaweb/osichat/templates/test.html +++ /dev/null @@ -1,3 +0,0 @@ -
- Hiiii -
\ No newline at end of file diff --git a/osinaweb/osinacore/templates/index.html b/osinaweb/osinacore/templates/index.html index ff9c2d12..22c74995 100644 --- a/osinaweb/osinacore/templates/index.html +++ b/osinaweb/osinacore/templates/index.html @@ -361,27 +361,6 @@ - - - diff --git a/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc b/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc index ddc7ebc0..e0b06112 100644 Binary files a/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc and b/osinaweb/osinaweb/__pycache__/settings.cpython-310.pyc differ diff --git a/osinaweb/osinaweb/settings.py b/osinaweb/osinaweb/settings.py index 74450788..3eb4444b 100644 --- a/osinaweb/osinaweb/settings.py +++ b/osinaweb/osinaweb/settings.py @@ -79,7 +79,6 @@ TEMPLATES = [ 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ - 'osinacore.custom_context.utilities', 'customercore.custom_context.utilities', 'django.template.context_processors.debug', diff --git a/osinaweb/static/js/osichat/chat-toggle.js b/osinaweb/static/js/osichat/chat-toggle.js new file mode 100644 index 00000000..5bba29b7 --- /dev/null +++ b/osinaweb/static/js/osichat/chat-toggle.js @@ -0,0 +1,18 @@ +console.log('toggle me'); +document.addEventListener('DOMContentLoaded', function() { + const openChatButton = document.getElementById('openChatContainer'); + const chatContainer = document.getElementById('chatContainer'); + const closeChatButton = document.getElementById('closeChatContainer'); + + openChatButton.addEventListener('click', function() { + chatContainer.classList.remove('hidden'); + openChatButton.classList.add('hidden'); + closeChatButton.classList.remove('hidden'); + }); + + closeChatButton.addEventListener('click', function() { + chatContainer.classList.add('hidden'); + openChatButton.classList.remove('hidden'); + closeChatButton.classList.add('hidden'); + }); +}) \ No newline at end of file diff --git a/osinaweb/static/js/osichat/conversation.js b/osinaweb/static/js/osichat/conversation.js new file mode 100644 index 00000000..a09e4ad3 --- /dev/null +++ b/osinaweb/static/js/osichat/conversation.js @@ -0,0 +1,87 @@ + +const chat_ws_scheme = window.location.protocol === "https:" ? "wss" : "ws"; +const domain = "192.168.1.111:8000"; +const sessionId = document.getElementById('session_id').textContent.trim(); +console.log(sessionId); +const osichatSocketUrl = `${chat_ws_scheme}://${domain}/ws/osichat/${sessionId}/`; +const osichatSocket = new WebSocket(osichatSocketUrl); + + +osichatSocket.onopen = () => { + console.log('WebSocket connection to osichat established'); +}; + +osichatSocket.onmessage = function(e) { + const data = JSON.parse(e.data); + if (data.event_type === 'load_chat') { + const chatDiv = document.getElementById('osichat'); + const html = data.html; + chatDiv.innerHTML = html; + + // Append the toggle tag + const script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = `http://${domain}/static/js/osichat/chat-toggle.js`; + chatDiv.appendChild(script); + + const startChatContainer = document.getElementById('startChat'); + // If this container exists then the template returned was start-conversation.html, no conversation yet. + if (startChatContainer) { + startChatContainer.addEventListener('submit', function(event) { + event.preventDefault(); + + const guestName = event.target.elements.guest_name.value; + const guestMobileNumber = event.target.elements.guest_mobile_number.value; + + const eventMessage = { + 'event_type': 'start_conversation', + 'guest_name': guestName, + 'guest_mobile_number': guestMobileNumber, + }; + + osichatSocket.send(JSON.stringify(eventMessage)); + + event.target.reset(); + }); + } + + const sendMessageContainer = document.getElementById('sendMessage'); + if (sendMessageContainer) { + sendMessageContainer.addEventListener('submit', function(event) { + event.preventDefault(); + + const message = event.target.elements.message.value; + + const eventMessage = { + 'event_type': 'send_message', + 'message': message, + + }; + + osichatSocket.send(JSON.stringify(eventMessage)); + + event.target.reset(); + }); + } + + } else if (data.event_type === 'start_conversation') { + const chatDiv = document.getElementById('startChatContainer'); + const html = data.html; + chatDiv.innerHTML = html; + + } else if (data.event_type === 'send_message') { + const messagesDiv = document.getElementById('messages'); + const html = data.html; + messagesDiv.insertAdjacentHTML('beforeend', html); + + } +}; + +osichatSocket.onclose = () => { + console.log('WebSocket connection to osichat closed'); +}; + +osichatSocket.onerror = (error) => { + console.log('WebSocket error:', error); +}; + diff --git a/osinaweb/support/.DS_Store b/osinaweb/support/.DS_Store index 34733c74..9685a61a 100644 Binary files a/osinaweb/support/.DS_Store and b/osinaweb/support/.DS_Store differ diff --git a/osinaweb/support/templates/.DS_Store b/osinaweb/support/templates/.DS_Store index e0c93cee..98c0dbf4 100644 Binary files a/osinaweb/support/templates/.DS_Store and b/osinaweb/support/templates/.DS_Store differ diff --git a/osinaweb/support/templates/details_templates/.DS_Store b/osinaweb/support/templates/details_templates/.DS_Store index 097fe15c..c35889ce 100644 Binary files a/osinaweb/support/templates/details_templates/.DS_Store and b/osinaweb/support/templates/details_templates/.DS_Store differ