Skip to content

Commit

Permalink
fix(channels): fixes for smooch and messenger (#229)
Browse files Browse the repository at this point in the history
* fix(messenger): fix fetch pageId when in live mode (#227)

* fix(smooch): fix buttons (#230)

* fix

Co-authored-by: Samuel Massé <59894025+samuelmasse@users.noreply.github.com>
Co-authored-by: Samuel Massé <samuelmasse4@gmail.com>
  • Loading branch information
3 people authored Nov 2, 2021
1 parent afa9ecd commit 42a1d5b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
9 changes: 7 additions & 2 deletions packages/server/src/channels/messenger/conduit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ export class MessengerConduit extends ConduitInstance<MessengerConfig, Messenger
const provider = await this.app.providers.getById(conduit!.providerId)
const channel = this.app.channels.getById(conduit!.channelId) as MessengerChannel

const pageId = await this.client.getPageId()
channel.registerPageId(pageId, provider!.name)
try {
const pageId = await this.client.getPageId()
channel.registerPageId(pageId, provider!.name)
} catch {
// when in live mode this call can fail. we can work around it for new users since they are supposed to use the botId in the url
// we don't show an error because this is correct usage
}
}

await this.printWebhook()
Expand Down
4 changes: 3 additions & 1 deletion packages/server/src/channels/smooch/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export class SmoochChannel extends Channel<SmoochConduit> {

if (req.headers['x-api-key'] === conduit.secret) {
const body = req.body as SmoochPayload
for (const message of body.messages) {

// postbacks is used when a button is clicked
for (const message of body.messages || body.postbacks) {
await conduit.receive({ context: body, message })
}
res.sendStatus(200)
Expand Down
26 changes: 22 additions & 4 deletions packages/server/src/channels/smooch/conduit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { ChannelContext } from '../base/context'
import { CardToCarouselRenderer } from '../base/renderers/card'
import { DropdownToChoicesRenderer } from '../base/renderers/dropdown'
import { SmoochConfig } from './config'
import { SmoochMessage, SmoochPayload, SmoochContext, SmoochWebhook } from './context'
import { SmoochMessage, SmoochPayload, SmoochContext, SmoochWebhook, SmoochAction } from './context'
import { SmoochRenderers } from './renderers'
import { SmoochSenders } from './senders'

export const SAY_PREFIX = 'say::'
export const POSTBACK_PREFIX = 'postback::'

export class SmoochConduit extends ConduitInstance<SmoochConfig, SmoochContext> {
private smooch: any
public secret!: string
Expand All @@ -31,7 +34,7 @@ export class SmoochConduit extends ConduitInstance<SmoochConfig, SmoochContext>
webhook = (
await this.smooch.webhooks.create({
target,
triggers: ['message:appUser']
triggers: ['message:appUser', 'postback']
})
).webhook
}
Expand Down Expand Up @@ -62,9 +65,24 @@ export class SmoochConduit extends ConduitInstance<SmoochConfig, SmoochContext>
return SmoochSenders
}

public async extractEndpoint(payload: { context: SmoochPayload; message: SmoochMessage }): Promise<EndpointContent> {
public async extractEndpoint(payload: {
context: SmoochPayload
message: SmoochMessage
action: SmoochAction
}): Promise<EndpointContent> {
const postback = payload.message.action?.payload
let content

if (postback?.startsWith(SAY_PREFIX)) {
content = { type: 'say_something', text: postback.replace(SAY_PREFIX, '') }
} else if (postback?.startsWith(POSTBACK_PREFIX)) {
content = { type: 'postback', payload: postback.replace(POSTBACK_PREFIX, '') }
} else {
content = { type: 'text', text: payload.message.text }
}

return {
content: { type: 'text', text: payload.message.text },
content,
thread: payload.context.conversation._id,
sender: payload.context.appUser._id
}
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/channels/smooch/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface SmoochPayload {
app: { _id: string }
version: '1.1'
messages: SmoochMessage[]
postbacks: SmoochMessage[]
appUser: SmoochAppUser
conversation: { _id: string }
}
Expand Down Expand Up @@ -51,6 +52,7 @@ export interface SmoochMessage {
/** web, messenger, telegram, wechat, twilio, etc */
type: string
}
action?: SmoochAction
}

export interface SmoochCard {
Expand Down
9 changes: 7 additions & 2 deletions packages/server/src/channels/smooch/renderers/carousel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ActionOpenURL, ActionPostback, ActionSaySomething, CardContent, CarouselContent } from '../../../content/types'
import { CarouselContext, CarouselRenderer } from '../../base/renderers/carousel'
import { POSTBACK_PREFIX, SAY_PREFIX } from '../conduit'
import { SmoochCard, SmoochAction, SmoochContext } from '../context'

type Context = CarouselContext<SmoochContext> & {
Expand Down Expand Up @@ -28,12 +29,16 @@ export class SmoochCarouselRenderer extends CarouselRenderer {
context.actions.push({
text: button.title,
type: 'postback',
payload: button.payload
payload: `${POSTBACK_PREFIX}${button.payload}`
})
}

renderButtonSay(context: Context, button: ActionSaySomething) {
// TODO: not supported?
context.actions.push({
text: button.title,
type: 'postback',
payload: `${SAY_PREFIX}${button.text}`
})
}

endRenderCard(context: Context, card: CardContent) {
Expand Down

0 comments on commit 42a1d5b

Please sign in to comment.