Q&A

Bot To Read Own Push Message

Good day,

First of all, I am a total newbie when it comes to programming and I have close to no experience with it.

However, I would like to know if there are any ways for the Bot to read it's own Push Messages. Currently, there is a different operation elsewhere which is pushing text messages to the Webhook on Heroku

POST https://api.line.me/v2/bot/message/push Header Content-Type : application/json Authourization : Bearer <channel_access_token> Body Raw JSON : {"to": "<user>", "messages":[{"type":"text","text":"This is a message"}]}

Thus, I would like the Bot to catch this message as an event and Read the information "text" and react/reply to that message. Is there any way to do it, or are there better recommended ways to go about this issue?

Using Python 3.10.0

  • 0
  • 5
  • 627
  • twitter facebook

Oh, I understand what you want to do. for example, how about creating a flask endpoint and accessing it to send message and do something at once? I think it's not a good idea, but you can do what you want to do. like

@app.route("/example", methods=["POST])
def receive_message():
    // check auth
    // ...
    // get request json
    to = request.json["to"]
    messages = request.json["messages"]
    // write something (send message?)
    // ...

and post

POST https://YOUR-DOMAIN/example
Header
Content-Type : application/json
Authourization : something
Body
Raw JSON : {"to": "<user>", "messages":[{"type":"text","text":"This is a message"}]}
  • 0

Hi ksyt,

I apologize for not being clear enough.

Here I have the information which I plugged into Postman to send to the Line API server

POST https://api.line.me/v2/bot/message/push
Header
Content-Type : application/json
Authourization : Bearer <channel_access_token>
Body
Raw JSON : {"to": "<user>", "messages":[{"type":"text","text":"This is a message"}]}

So, on my mobile device, I would get the message "This is a message" which was sent by the Bot. And, on my code, I would like to catch that event and process that information.

However, based on your previous post, it seems like the Bot is unable to catch its own messages?

Best regards.

  • 0

it's not possible bacause the data you want is sent by bot's own and it's unnecessary to use api to achive it. After bot sending message, you can process the message as you want. if you want to call something after calling api, you can create a new class that wraps |line_bot_api|.

message = 'Hello there! \nThis is your user ID: '+ event.source.user_id + '\n' + 'The is the group ID: '+ event.source.group_id
line_bot_api.push_message(event.source.group_id, TextSendMessage(text=message))
// you can call any function that uses |message|
  • 0

Hi ksyt,

What do you mean that bot reads its own push message?

When the bot sends a message to the group, I want to be able to catch the contents of the message that was just sent.

If it was a user who typed it on the chatbox, then we can catch it by using this

@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):

#if event.message.text == 'Show ID':
    line_bot_api.push_message(event.source.group_id, TextSendMessage(text='Hello there! \nThis is your user ID: '+ event.source.user_id + '\n' + 'The is the group ID: '+ event.source.group_id))

However, if it was the bot's own message, how do we catch it? That is the question I was trying to ask, and would you have idea how I could achieve it, or is it not possible?

Best regards.

  • 0

What do you mean that bot reads its own push message?

when bot sends message to user, we need to use /v2/bot/message/push and when user sends message to the bot, webhook comes to your webhook endpoint.

please read documentation about messageing api. https://developers.line.biz/en/docs/messaging-api/

do you use https://github.com/line/line-bot-sdk-python? It'd be helpful!

  • 0
本当によろしいですか? question.vm