Question : Is there any way to Call Push Messages from Website POST Request
thirza258 opened this issue · 4 comments
#Question : Is there any way to Call Push Messages from Website POST Request
Description
I am trying to call the push_message function in my Django project when a POST request is made to my website. The push_message function should send a push message using the LINE Bot API. However, I am encountering issues with validation errors. Below is my current implementation.
Current Implementation example
Views
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from linebot import LineBotApi, LineBotApiError
from linebot.models import TextSendMessage
import json
from .models import TextMessageUser
from .serializers import TextMessageUserSerializer
# Initialize Line Bot API with your Channel Access Token
line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
class TextMessageUserViews(APIView):
def get(self, request):
try:
text_message_user = TextMessageUser.objects.all()
serializer = TextMessageUserSerializer(text_message_user, many=True)
return Response({"text_message_user": serializer.data}, status=status.HTTP_200_OK)
except TextMessageUser.DoesNotExist:
return Response({"error": "TextMessageUser not found"}, status=status.HTTP_404_NOT_FOUND)
def post(self, request):
try:
id = request.data["id"]
comment = request.data["comment"]
text_message_user = TextMessageUser.objects.create(id=id, comment=comment)
push_message()
text_message_user.save()
return Response({"text_message_user": "created"}, status=status.HTTP_201_CREATED)
except KeyError:
return Response({"error": "Invalid request"}, status=status.HTTP_400_BAD_REQUEST)
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
user_msg = event.message.text
// { code for reply message}
@csrf_exempt
def push_message():
try:
line_bot_api.push_message(
PushMessageRequest(
to= " my user_id from event.source.user_id ",
messages=[TextMessage(text="Hello, world!")]
)
)
except LineBotApiError as e:
print(e)by the way, the code above not the real implementation but the example for real implementation
Problem
I am encountering the following error when trying to call the push_message function:
pydantic.v1.error_wrappers.ValidationError: 1 validation error for PushMessageRequest
messages -> 0
value is not a valid dict (type=type_error.dict)
Any assistance or guidance on resolving this issue would be greatly appreciated. Thank you!
Thank you for using line-bot-sdk-python, @thirza258 !
Is the import section of the sample you provided correct? PushMessageRequest exists only in linebot.v3.messaging.
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
PushMessageRequest,
TextMessage
...
)It probably needs to be imported like this. Also, I think you need to define the client as shown in the README sample.
https://github.com/line/line-bot-sdk-python?tab=readme-ov-file#synopsis
and ...
The sample in README:
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text=event.message.text)]
)should work if rewritten like this, if you want to use push:
line-bot-sdk-python/examples/flask-kitchensink/app.py
Lines 213 to 219 in bdda65f
I'm sorry that's true (looks like i'm making mistake on making the question) I'm using the following import statement in my implementation:
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
PushMessageRequest,
TextMessage
...
)
My implementation is primarily derived from the examples/flask-kitchensink/app.py example.
Based on the examples, I need to first send a "push" message to the Line bot by chatting "push" to it:
@handler.add(MessageEvent, message=TextMessageContent)
def handle_text_message(event):
text = event.message.text
with ApiClient(configuration) as api_client:
line_bot_api = MessagingApi(api_client)
{other code}
elif text == 'push':
line_bot_api.push_message(
PushMessageRequest(
to=event.source.user_id,
messages=[TextMessage(text='PUSH!')]
)
)
This will push the messages. What I expect is that when a user sends a POST request (example: making a comment) to my website that is the webhook website too, the push message gets called and sends the response from the Line bot to me as a chat. It functions like notifications but in the form of a chat from the Line bot. Is there any way to do that?
We do not accept questions about general web application development. Sorry. You can save the userId in your application or database, and when your web app receives a request(like making a comment), your application can send a push request to the LINE Messaging API.
If the issue has been resolved, please close the issue.
Thank you so much for your assistance and taking the time to answer this question. I just figured it out. It turned out i'm using TextMessage instead of TextSendMessage. :)