Skip to content

Commit

Permalink
Merge 63d5692 into 18430f9
Browse files Browse the repository at this point in the history
  • Loading branch information
webNeat committed Jul 5, 2021
2 parents 18430f9 + 63d5692 commit 4e41aef
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 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.4",
"version": "1.0.0-beta.5",
"license": "MIT",
"description": "A library to create chat bots for Messenger",
"module": "dist/messenger-chat.esm.js",
Expand Down
24 changes: 4 additions & 20 deletions src/bot/handle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {EventEntry, WebhookEvent} from '../events/types'
import {Reply} from '../messages/types'
import {BotConfig} from './types'
import {send} from './send'
import {BotConfig, EventEntry, WebhookEvent} from '../types'

export async function handle(config: BotConfig, event: WebhookEvent) {
if (event.object !== 'page') {
Expand All @@ -17,33 +16,18 @@ 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 responses = config.handle({...messaging, context, setContext, getUserFields})
let responses = config.handle({...messaging, contextKey, context, setContext, getUserFields})
if (responses instanceof Promise) {
responses = await responses
}
if (responses && !Array.isArray(responses)) {
responses = [responses]
}
if (responses) {
await Promise.all(
responses.map(response =>
sendReply(config, {
messaging_type: 'RESPONSE',
recipient: sender,
message: response,
})
)
)
await Promise.all(responses.map(response => send(config, sender.id, response)))
}
}

async function sendReply({axiosInstance, accessToken}: BotConfig, reply: Reply) {
return axiosInstance.post(
`https://graph.facebook.com/v11.0/me/messages?access_token=${accessToken}`,
reply
)
}

async function loadUserFields({axiosInstance, accessToken}: BotConfig, sender: {id: string}) {
const res = await axiosInstance.get(
`https://graph.facebook.com/v11.0/${sender.id}?access_token=${accessToken}&fields=id,first_name,last_name,profile_pic`
Expand Down
2 changes: 2 additions & 0 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios'
import {send} from './send'
import {handle} from './handle'
import {verify} from './verify'
import {Bot, BotConfig} from './types'
Expand All @@ -21,5 +22,6 @@ export function bot(config: Partial<BotConfig>): Bot {
return {
verify: query => verify(config as BotConfig, query),
handle: event => handle(config as BotConfig, event),
send: (id, message) => send(config as BotConfig, id, message),
}
}
14 changes: 14 additions & 0 deletions src/bot/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Message} from '../messages/types'
import {BotConfig} from './types'

export async function send(config: BotConfig, recipientId: string, message: Message) {
const {axiosInstance, accessToken} = config
return axiosInstance.post(
`https://graph.facebook.com/v11.0/me/messages?access_token=${accessToken}`,
{
messaging_type: 'RESPONSE',
recipient: {id: recipientId},
message,
}
)
}
2 changes: 2 additions & 0 deletions src/bot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type BotConfig = {
export interface Bot {
verify(query: VerificationQuery): string
handle(event: WebhookEvent): Promise<void>
send(recipientId: string, message: Message): Promise<any>
}

export type VerificationQuery = {
Expand All @@ -26,6 +27,7 @@ export type VerificationQuery = {
}

export type ChatEntry = MessagingItem & {
contextKey: string
context: any
setContext: (value: Record<string, any>) => void
getUserFields: () => Promise<UserFields>
Expand Down

0 comments on commit 4e41aef

Please sign in to comment.