line-bot-sdk-nodejs icon indicating copy to clipboard operation
line-bot-sdk-nodejs copied to clipboard

Property 'replyToken' does not exist on type 'WebhookEvent' in typescript

Open adisak1618 opened this issue 7 years ago • 2 comments

export default async (event: WebhookEvent): Promise<any> => {
  console.log('event', event);
  const msg: TextMessage = { type: "text", text: 'hello world!' };
  return client.replyMessage(event.replyToken, msg);
}

i already check in this lib about WebhookEvent type. it already define 'replyToken' as a property already but i don't know why it still complain.

i think it define here at https://github.com/line/line-bot-sdk-nodejs/blob/master/lib/types.ts#L98

adisak1618 avatar May 07 '19 07:05 adisak1618

WebhookEvent is not derived from ReplyableEvent.

export type WebhookEvent =
  | MessageEvent
  | FollowEvent
  | UnfollowEvent
  | JoinEvent
  | LeaveEvent
  | MemberJoinEvent
  | MemberLeaveEvent
  | PostbackEvent
  | BeaconEvent
  | AccountLinkEvent
  | DeviceLinkEvent
  | DeviceUnlinkEvent;

For example, UnfollowEvent is not replyable.

As a result, you need to check the type of event before taking a reply token.

function handleEvent(event: line.WebhookEvent) {
  if (event.type !== 'message' || event.message.type !== 'text') {
    // ignore non-text-message event
    return Promise.resolve(null);
  }

  console.log(`Received message: ${event.message.text}`);

  // create a echoing text message
  const echo: line.TextMessage = { type: 'text', text: event.message.text };

  // use reply API
  return client.replyMessage(event.replyToken, echo);
}

tokuhirom avatar May 07 '19 08:05 tokuhirom

@tokuhirom ahh i got it thank a lot

adisak1618 avatar May 10 '19 16:05 adisak1618