Anonymous
Anonymous

Reputation: 1

Line webhook not getting active programatically

i am trying to add webhook to line programatically via channel access token. My code sets the webhook to line, but the webhook does gets enabled.

Below is my code for it:

async function setWebhook(channelAccessToken) {
  const config = {
    headers: {
      "Content-Type": "application/json",
      "x-line-signature": "2e7306a15c5c775341f003d65fe62d72",
      Authorization: `Bearer ${channelAccessToken}`,
    },
  };
  const data = {
    endpoint: `${baseUrl}/line/webhook`
  };
  try {
    const result = await axios.put(
      "https://api.line.me/v2/bot/channel/webhook/endpoint",
      data,
      config
    );
    console.log("result.data ============= ", result.data);
    return result;
  } catch (error) {
    // console.log("tester ========== ", error)
    throw new Error("Error setting webhook: " + error.message);
  }
}

But when i hit the below api, it gives response which shows that webhook is set but not enabled.

const verifyResult = await axios.get(
  "https://api.line.me/v2/bot/channel/webhook/endpoint",
  config
);

Below is the response:

{
    "endpoint": "https://1f29-45-248-162-227.ngrok-free.app/line/webhook",
    "active": false
}

Do anyone have any idea, how to solve it, any small help will be really appreciated !!!

I have tried to pass enable:true while setting up the webhook, but that also didn't worked. Can somebody help?

Upvotes: 0

Views: 266

Answers (2)

subdeveloper
subdeveloper

Reputation: 2668

A wild guess, maybe you did not enable the webhook by clicking Use Webhook see here?:


Enable webhook screenshot

Upvotes: 0

Yuvaraj M
Yuvaraj M

Reputation: 4608

You have to verify the webhook url after creating.

It can be done in two ways,

  1. Using axios
const response = await axios.post(
    'https://api.line.me/v2/bot/channel/webhook/test',
    {
        endpoint: `${baseUrl}/line/webhook`
    },
    {
        headers: {
            Authorization: 'Bearer {CHANNEL_ACCESS_TOKEN}',
            'Content-Type': 'application/json'
        }
    }
);
  1. Using LINE Developers Console, click the webhook URL's Verify button to perform the verification.

enter image description here

LINE Doc for verifying webhook url

Upvotes: 0

Related Questions