Skip to content

Commit

Permalink
Release 1.0.0-beta.3
Browse files Browse the repository at this point in the history
  • Loading branch information
webNeat committed Jun 28, 2021
1 parent caee14d commit e6cf8e9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "messenger-chat",
"author": "Amine Ben hammou",
"version": "1.0.0-beta.2",
"version": "1.0.0-beta.3",
"license": "MIT",
"description": "A library to create chat bots for Messenger",
"module": "dist/messenger-chat.esm.js",
Expand Down
25 changes: 16 additions & 9 deletions src/bot/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ async function handleEntry(config: BotConfig, entry: EventEntry) {
const context = (await storage.get(contextKey)) || initialContext
const setContext = (value: any) => storage.set(contextKey, value)
const getUserFields = () => loadUserFields(config, sender)
let response = config.handle({...messaging, context, setContext, getUserFields})
if (response instanceof Promise) {
response = await response
let responses = config.handle({...messaging, context, setContext, getUserFields})
if (responses instanceof Promise) {
responses = await responses
}
if (response) {
await sendReply(config, {
messaging_type: 'RESPONSE',
recipient: sender,
message: response,
})
if (responses && !Array.isArray(responses)) {
responses = [responses]
}
if (responses) {
await Promise.all(
responses.map(response =>
sendReply(config, {
messaging_type: 'RESPONSE',
recipient: sender,
message: response,
})
)
)
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/bot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import {Message} from '../messages/types'
import {ContextStorage} from '../storages/types'
import {WebhookEvent, MessagingItem} from '../events/types'

type OptionalOneOrMany<T> = T | T[] | undefined

export type BotConfig = {
accessToken: string
verifyToken: string
initialContext: any
storage: ContextStorage
handle: (entry: ChatEntry) => Message | undefined | Promise<Message | undefined>
handle: (entry: ChatEntry) => OptionalOneOrMany<Message> | Promise<OptionalOneOrMany<Message>>
axiosInstance: AxiosInstance
}

Expand Down

0 comments on commit e6cf8e9

Please sign in to comment.