Send Messages using LINE app

Hello, I am currently trying to send messages using the LINE app, but I could not find a way to do it. This is my code:

const axios = require('axios');
const querystring = require('querystring');

axios({
    method: 'post',
    url: 'https://notify-api.line.me/api/notify',
    headers: {
      'Authorization': 'Bearer ' + 'ACCESS_TOKEN',
      'Content-Type': 'application/x-www-form-urlencoded',
      'Access-Control-Allow-Origin': '*'
    },
    data: querystring.stringify({
      message: 'something to push',
    })
  })
.then( function(res) {
  console.log(res.data);
})
.catch( function(err) {
  console.error(err);
});

I appreciate your help. Regards.

Hi @miedumni

First off, welcome to the Pipedream community. Happy to have you!

Customizing the default scaffolding that comes with an app can be a bit confusing at first, but once you get the hang of it, it opens up many many doors for you.

Let’s start with the default scaffolding when you start the action from scratch:


import { axios } from "@pipedream/platform"
export default defineComponent({
  props: {
    line: {
      type: "app",
      app: "line",
    }
  },
  async run({steps, $}) {
    return await axios($, {
      url: `https://notify-api.line.me/api/status`,
      headers: {
        Authorization: `Bearer ${this.line.$auth.oauth_access_token}`,
      },
    })
  },
}

Pipedream handles the authorization of the request for you. So you don’t need to modify the headers whatsoever.

The only portion you need to concern yourself with is to modifying the endpoint and body of the request. So if you’re trying to send the /api/notify endpoint, and pass the { message: 'something to push' } body to that endpoint, then only modify the url, method, and the data of the request:

import { axios } from "@pipedream/platform"
export default defineComponent({
  props: {
    line: {
      type: "app",
      app: "line",
    }
  },
  async run({steps, $}) {
    return await axios($, {
      method: 'post',
      url: `https://notify-api.line.me/api/notify`,
      headers: {
        Authorization: `Bearer ${this.line.$auth.oauth_access_token}`,
      },
      data: { message: 'something to push' }
    })
  },
}

Axios will automatically handle the Content-Type header, and Access-Control-Allow-Origin is a browser concern, not a server side concern. Your axios request is executed within a server, not from your browser directly.

Hope this helps!

Hi Pierce, thanks for kind reply.

I used your code, but it is not working, sometimes I am getting error 401 or 400 bad request.

Should I do something maybe in the dashboard of line?

Thanks for your help.

Amazing job BTW, Just subscribed !!!

I forgot to mention, I connect and disconnect as well from the dashboard of LINE. So I don’t know if soething else can be useful.

Regards.

Hi @miedumni

Thanks for sharing the screenshot, that’s very helpful.

In every API response from a service, the HTTP status & the data in the payload can give us clues as to why it failed.

In this case your request failed because it’s expecting a message parameter:

I assumed that the message belonged in the body of the payload, in my example I hardcoded a string 'something to push' as the value.

But maybe the API requires the message in a different area like a query param? Or maybe the code was modified a bit?

Hi Pierce, Thank you again.
I could use it finally. The syntax it worked for me was:

data: 'message="Something to Push"',

But, I am getting in the app:

Pipedream: Something to Push

There is a way to get rid of the "Pipedream:" in the message?

Thanks again Pierce. Regards.

I just find that this is generated when the Token is created.

Thank you, very Much !!!

1 Like