Skip to content

Latest commit

 

History

History
585 lines (499 loc) · 10.3 KB

File metadata and controls

585 lines (499 loc) · 10.3 KB

messaging-api-line

Messaging API client for LINE

Table of Contents

Installation

npm i --save messaging-api-line

or

yarn add messaging-api-line

Usage

Initialize

import { LINEClient } from 'messaging-api-line';

// get accessToken and channelSecret from LINE developers website
const client = LINEClient.connect(accessToken, channelSecret);

Call API

async function() {
  await client.pushText(id, text);
}

or

client.pushText(id, text).then(() => {
  // do something
});

API Reference

All methods return a Promise.

Reply API

Official Docs

reply(token, message)

client.reply('1qwyg56uj', [
  {
    type: 'text',
    text: 'Hello!',
  },
]);

replyText(token, text)

client.reply('1qwyg56uj', 'Hello!');

replyImage(token, imageUrl, previewImageUrl)

client.replyImage(
  '1qwyg56uj',
  'https://example.com/original.jpg',
  'https://example.com/preview.jpg'
);

replyVideo(token, vedioUrl, previewImageUrl)

client.replyVideo(
  '1qwyg56uj',
  'https://example.com/original.mp4',
  'https://example.com/preview.jpg'
);

replyAudio(token, audioUrl, duration)

client.replyAudio('1qwyg56uj', 'https://example.com/original.m4a', 240000);

replyLocation(token, location)

client.replyLocation('1qwyg56uj', {
  title: 'my location',
  address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
  latitude: 35.65910807942215,
  longitude: 139.70372892916203,
});

replySticker(token, packageId, stickerId)

client.replySticker('1qwyg56uj', '1', '1');

replyImagemap(token, altText, imagemap)

client.replyImagemap(
  '1qwyg56uj',
  'this is an imagemap',
  {
    baseUrl: 'https://example.com/bot/images/rm001',
    baseHeight: 1040,
    baseWidth: 1040,
    actions: [
      {
        type: 'uri',
        linkUri: 'https://example.com/',
        area: {
          x: 0,
          y: 0,
          width: 520,
          height: 1040,
        },
      },
      {
        type: 'message',
        text: 'hello',
        area: {
          x: 520,
          y: 0,
          width: 520,
          height: 1040,
        },
      },
    ],
  }
);

Official Docs

replyTemplate(token, altText, template)

client.replyTemplate(
  '1qwyg56uj',
  'this is a template',
  {
    type: 'buttons',
    thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
    title: 'Menu',
    text: 'Please select',
    actions: [
      {
        type: 'postback',
        label: 'Buy',
        data: 'action=buy&itemid=123',
      },
      {
        type: 'postback',
        label: 'Add to cart',
        data: 'action=add&itemid=123',
      },
      {
        type: 'uri',
        label: 'View detail',
        uri: 'http://example.com/page/123',
      },
    ],
  }
);

replyButtonTemplate(token, altText, buttonTemplate)

client.replyButtonTemplate(
  '1qwyg56uj',
  'this is a template',
  {
    thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
    title: 'Menu',
    text: 'Please select',
    actions: [
      {
        type: 'postback',
        label: 'Buy',
        data: 'action=buy&itemid=123',
      },
      {
        type: 'postback',
        label: 'Add to cart',
        data: 'action=add&itemid=123',
      },
      {
        type: 'uri',
        label: 'View detail',
        uri: 'http://example.com/page/123',
      },
    ],
  }
);

replyConfirmTemplate(token, altText, confirmTemplate)

client.replyConfirmTemplate(
  '1qwyg56uj',
  'this is a confirm template',
  {
    text: 'Are you sure?',
    actions: [
      {
        type: 'message',
        label: 'Yes',
        text: 'yes',
      },
      {
        type: 'message',
        label: 'No',
        text: 'no',
      },
    ],
  }
);

replyCarouselTemplate(token, altText, carouselItems)

client.replyCarouselTemplate(
  '1qwyg56uj',
  'this is a carousel template',
  [
    {
      thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
      title: 'this is menu',
      text: 'description',
      actions: [
        {
          type: 'postback',
          label: 'Buy',
          data: 'action=buy&itemid=111',
        },
        {
          type: 'postback',
          label: 'Add to cart',
          data: 'action=add&itemid=111',
        },
        {
          type: 'uri',
          label: 'View detail',
          uri: 'http://example.com/page/111',
        },
      ],
    },
    {
      thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
      title: 'this is menu',
      text: 'description',
      actions: [
        {
          type: 'postback',
          label: 'Buy',
          data: 'action=buy&itemid=222',
        },
        {
          type: 'postback',
          label: 'Add to cart',
          data: 'action=add&itemid=222',
        },
        {
          type: 'uri',
          label: 'View detail',
          uri: 'http://example.com/page/222',
        },
      ],
    },
  ]
);

Push API

Official Docs

push(userId, messages)

client.push('1', [
  {
    type: 'text',
    text: 'Hello!',
  },
]);

pushText(userId, text)

client.pushText('1', 'Hello!');

pushImage(userId, imageUrl, previewImageUrl)

client.pushImage(
  '1',
  'https://example.com/original.jpg',
  'https://example.com/preview.jpg'
);

pushVideo(userId, vedioUrl, previewImageUrl)

client.pushVideo(
  '1',
  'https://example.com/original.mp4',
  'https://example.com/preview.jpg'
);

pushAudio(userId, audioUrl, duration)

client.pushAudio('1', 'https://example.com/original.m4a', 240000);

pushLocation(userId, location)

client.pushLocation('1', {
  title: 'my location',
  address: '〒150-0002 東京都渋谷区渋谷2丁目21−1',
  latitude: 35.65910807942215,
  longitude: 139.70372892916203,
});

pushSticker(userId, packageId, stickerId)

client.pushSticker('1', '1', '1');

pushImagemap(userId, altText, imagemap)

client.pushImagemap(
  '1',
  'this is an imagemap',
  {
    baseUrl: 'https://example.com/bot/images/rm001',
    baseHeight: 1040,
    baseWidth: 1040,
    actions: [
      {
        type: 'uri',
        linkUri: 'https://example.com/',
        area: {
          x: 0,
          y: 0,
          width: 520,
          height: 1040,
        },
      },
      {
        type: 'message',
        text: 'hello',
        area: {
          x: 520,
          y: 0,
          width: 520,
          height: 1040,
        },
      },
    ],
  }
);

Official Docs

pushTemplate(userId, altText, template)

client.pushTemplate(
  '1',
  'this is a template',
  {
    type: 'buttons',
    thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
    title: 'Menu',
    text: 'Please select',
    actions: [
      {
        type: 'postback',
        label: 'Buy',
        data: 'action=buy&itemid=123',
      },
      {
        type: 'postback',
        label: 'Add to cart',
        data: 'action=add&itemid=123',
      },
      {
        type: 'uri',
        label: 'View detail',
        uri: 'http://example.com/page/123',
      },
    ],
  }
);

pushButtonTemplate(userId, altText, buttonTemplate)

client.pushButtonTemplate(
  '1',
  'this is a template',
  {
    thumbnailImageUrl: 'https://example.com/bot/images/image.jpg',
    title: 'Menu',
    text: 'Please select',
    actions: [
      {
        type: 'postback',
        label: 'Buy',
        data: 'action=buy&itemid=123',
      },
      {
        type: 'postback',
        label: 'Add to cart',
        data: 'action=add&itemid=123',
      },
      {
        type: 'uri',
        label: 'View detail',
        uri: 'http://example.com/page/123',
      },
    ],
  }
);

pushConfirmTemplate(userId, altText, confirmTemplate)

client.pushConfirmTemplate(
  '1',
  'this is a confirm template',
  {
    text: 'Are you sure?',
    actions: [
      {
        type: 'message',
        label: 'Yes',
        text: 'yes',
      },
      {
        type: 'message',
        label: 'No',
        text: 'no',
      },
    ],
  }
);

pushCarouselTemplate(userId, altText, carouselItems)

client.pushCarouselTemplate(
  '1',
  'this is a carousel template',
  [
    {
      thumbnailImageUrl: 'https://example.com/bot/images/item1.jpg',
      title: 'this is menu',
      text: 'description',
      actions: [
        {
          type: 'postback',
          label: 'Buy',
          data: 'action=buy&itemid=111',
        },
        {
          type: 'postback',
          label: 'Add to cart',
          data: 'action=add&itemid=111',
        },
        {
          type: 'uri',
          label: 'View detail',
          uri: 'http://example.com/page/111',
        },
      ],
    },
    {
      thumbnailImageUrl: 'https://example.com/bot/images/item2.jpg',
      title: 'this is menu',
      text: 'description',
      actions: [
        {
          type: 'postback',
          label: 'Buy',
          data: 'action=buy&itemid=222',
        },
        {
          type: 'postback',
          label: 'Add to cart',
          data: 'action=add&itemid=222',
        },
        {
          type: 'uri',
          label: 'View detail',
          uri: 'http://example.com/page/222',
        },
      ],
    },
  ]
);

Multicast API

Official Docs

multicast(userIds, messages)

client.multicast(['1'], [
  {
    type: 'text',
    text: 'Hello!',
  },
]);

multicastText(userIds, text)

client.multicastText(['1'], 'Hello!');

Others

getUserProfile

Official Docs

leaveGroup

Official Docs

leaveRoom

Official Docs

isValidSignature

Official Docs

retrieveMessageContent

Official Docs