dashboard: userlarga message yozish uchun api qoshildi
This commit is contained in:
7
core/apps/dashboard/serializers/send_message.py
Normal file
7
core/apps/dashboard/serializers/send_message.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# rest framework
|
||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
|
class SendMessageSerializer(serializers.Serializer):
|
||||||
|
user_ids = serializers.ListField(child=serializers.IntegerField())
|
||||||
|
message = serializers.CharField()
|
||||||
@@ -35,6 +35,8 @@ from core.apps.dashboard.views.location import LocationViewSet, UserLocationView
|
|||||||
from core.apps.dashboard.views.support import SupportListApiView
|
from core.apps.dashboard.views.support import SupportListApiView
|
||||||
# distibuted products
|
# distibuted products
|
||||||
from core.apps.dashboard.views.dis_prod import DistributedProductListApiView
|
from core.apps.dashboard.views.dis_prod import DistributedProductListApiView
|
||||||
|
# send message
|
||||||
|
from core.apps.dashboard.views.send_message import SendMessageToEmployee
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
@@ -82,7 +84,10 @@ urlpatterns = [
|
|||||||
[
|
[
|
||||||
path('list/', DistributedProductListApiView.as_view(), name='distributed-product-list-api'),
|
path('list/', DistributedProductListApiView.as_view(), name='distributed-product-list-api'),
|
||||||
]
|
]
|
||||||
))
|
)),
|
||||||
|
|
||||||
|
# -------------- send message --------------
|
||||||
|
path('send_message/', SendMessageToEmployee.as_view()),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
38
core/apps/dashboard/views/send_message.py
Normal file
38
core/apps/dashboard/views/send_message.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# rest framework
|
||||||
|
from rest_framework import generics, permissions
|
||||||
|
|
||||||
|
|
||||||
|
# services
|
||||||
|
from core.services.send_telegram_msg import send_message
|
||||||
|
# accounts
|
||||||
|
from core.apps.accounts.models import User
|
||||||
|
#shared
|
||||||
|
from core.apps.shared.utils.response_mixin import ResponseMixin
|
||||||
|
# dashboard
|
||||||
|
from core.apps.dashboard.serializers.send_message import SendMessageSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class SendMessageToEmployee(generics.GenericAPIView, ResponseMixin):
|
||||||
|
serializer_class = SendMessageSerializer
|
||||||
|
permission_classes = [permissions.IsAdminUser]
|
||||||
|
queryset = User.objects.all()
|
||||||
|
|
||||||
|
def post(self, request):
|
||||||
|
try:
|
||||||
|
serializer = self.serializer_class(data=request.data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
data = serializer.validated_data
|
||||||
|
message = data.get('message')
|
||||||
|
user_ids = data.get('user_ids')
|
||||||
|
users = User.objects.filter(id__in=user_ids)
|
||||||
|
for user in users:
|
||||||
|
send_message(chat_id=user.telegram_id, message=message)
|
||||||
|
return self.success_response(
|
||||||
|
data={},
|
||||||
|
message="Xabar yuborildi"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return self.error_response(
|
||||||
|
data=str(e),
|
||||||
|
message="xatolik, backend dasturchi bilan boglaning"
|
||||||
|
)
|
||||||
@@ -31,3 +31,20 @@ def send_to_telegram(chat_id, order_id):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Telegram xatolik: {e}")
|
print(f"Telegram xatolik: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def send_message(chat_id, message):
|
||||||
|
bot_token = settings.BOT_TOKEN
|
||||||
|
|
||||||
|
try:
|
||||||
|
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
|
||||||
|
data = {
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"text": message
|
||||||
|
}
|
||||||
|
response = requests.post(url, data=data)
|
||||||
|
print(response.json())
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Telegram xatosi: {e}")
|
||||||
|
return False
|
||||||
Reference in New Issue
Block a user