Skip to content

Latest commit

 

History

History
225 lines (156 loc) · 6.16 KB

chat.md

File metadata and controls

225 lines (156 loc) · 6.16 KB

@aiteq/messenger-bot > Chat

Class: Chat

Provides methods for two-way bot-to-user communication. An instance of Chat is always passed to hear and event handlers to be used for interaction with the user. Note: all methods are non-blocking and call underlaying API asynchronously.

Index

Methods


Methods

ask(text, validator)

Asks the user with a plain TEXT message and returns user's response (TEXT or QUICK REPLY). If a validator is specified, the bot will automatically repeat the challenge until valid response.

As a validator you can use functions from validator.js package:

import * as validator from "validator";

bot.on(Webhook.Event.PERSISTENT_MENU, "menu-item-form", async (chat: Chat) => {
    //...
    let email: string = await chat.ask("Give me your email address, please", validator.isEmail)
    //...
});

The bot will automatically repeat the question until the user enters a valid email address.

Note: No events are emitted and no hear handlers called when the bot receives an answer to the question asked.

Parameters:

Param Type Description
text string a question
validator (text: string) => boolean optional validator function - returns true if the input is valid

Returns: Promise<string>


askWithMessage(messageOrBuilder, validator)

Asks the user with a message prepared manually or using message builder. It's necessary when we want to force the user to response using QUICK REPLY buttons.

If a validator is specified, the bot will automatically repeat the challenge until valid response.

Note: No events are emitted and no hear handlers called when the bot receives an answer to the question asked.

Type parameters:

T: stringQuickReplyPayload

Parameters:

Param Type Description
messageOrBuilder MessageMessageBuilder structured message or message builder
validator (text: string) => boolean optional validator function - returns true if the input is valid

Returns: Promise<T>


getPartnerId()

Returns an ID of the chat partner.

Returns: string


getUserProfile()

Returns user's profile containing public information.

Returns: Promise<UserProfile> - user's public profile information


markSeen()

Marks the last sent message as read.

Returns: Promise<Send.Response>


say(text)

The primary way to send a plain TEXT message to the user.

Parameters:

Param Type Description
text string a text to be send

Returns: Promise<Send.Response>


sendAudio(url, [reusable])

Sends an audio file.

Parameters:

Param Type Default value Description
url string a URL of the audio file
reusable boolean false controls whether the attachment can be reused later

Returns: Promise<Send.Response>


sendFile(url, [reusable])

Sends a file.

Parameters:

Param Type Default value Description
url string a URL of the file
reusable boolean false controls whether the attachment can be reused later

Returns: Promise<Send.Response>


sendImage(url, [reusable])

Sends an image.

Parameters:

Param Type Default value Description
url string - a URL of the image file
reusable boolean false controls whether the attachment can be reused later

Returns: Promise<Send.Response>


sendMessage(messageOrBuilder)

Sends a message prepared manually or using message builder.

Parameters:

Param Type Description
messageOrBuilder MessageMessageBuilder a structured message or message builder

Returns: Promise<Send.Response>


sendVideo(url, [reusable])

Sends a video file.

Parameters:

Param Type Default value Description
url string a URL of the video file
reusable boolean false controls whether the attachment can be reused later

Returns: Promise<Send.Response>


typingOff()

Turns typing indicator OFF.

Returns: Promise<Send.Response>


typingOn()

Turns typing indicator ON for 20 seconds or next message.

Returns: Promise<Send.Response>


wait()

Wait for the given seconds before next chat action.

bot.hear("order", async (chat: Chat) => {
    //...
    chat.wait(30).say("Sorry to disturb you yet again, but I want to ask you ...");
    //...
});

Parameters:

Param Type Default value Description
seconds number number of seconds to wait

Returns: this - for chaining