Skip to content

Commit

Permalink
✨ feat: botpress converse api integration now sends file title as cap…
Browse files Browse the repository at this point in the history
…tion #2072
  • Loading branch information
smashah committed Aug 31, 2021
1 parent 12c466e commit 030b679
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions src/cli/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import robots from "express-robots-txt";
import swaggerUi from 'swagger-ui-express';
import { default as axios } from 'axios'
import parseFunction from 'parse-function';
import { Client, ev, SimpleListener } from '..';
import { Client, ev, SimpleListener, ChatId } from '..';

export const app = express();
export const server = http.createServer(app);
Expand Down Expand Up @@ -158,44 +158,25 @@ export const setupMediaMiddleware : () => void = () => {
}

export const setupBotPressHandler : (cliConfig : cliFlags, client: Client) => void = (cliConfig : cliFlags, client: Client) => {
client.onMessage(async message=>{
const u = cliConfig.botPressUrl as string
const url = `${u.split("/").slice(0,u.split("/").findIndex(x=>x=="converse")).join("/")}/converse/${message.from.replace("@c.us","").replace("@g.us","")}`
const u = cliConfig.botPressUrl as string
const sendBotPressMessage = async (text:string, chatId : ChatId) => {
const url = `${u.split("/").slice(0,u.split("/").findIndex(x=>x=="converse")).join("/")}/converse/${chatId.replace("@c.us","").replace("@g.us","")}`
try {
let text = message.body;
switch(message.type) {
case 'location':
text = `${message.lat},${message.lng}`;
break;
case 'buttons_response':
text = message.selectedButtonId;
break;
case 'document':
case 'image':
case 'audio':
case 'ptt':
case 'video':
if(message.cloudUrl) text = message.cloudUrl;
break;
default:
text = message.body;
break;
}
const {data} = await axios.post(url, {
"type": "text",
text
})
const {responses} = data;
return await Promise.all(responses.filter(({type})=>type!="typing").map((response : any) => {
if(response.type=="text"){
return client.sendText(message.from, response.text)
return client.sendText(chatId, response.text)
}
if(response.type=="file"){
return client.sendFile(message.from, response.url, `file.${response.url.split(/[#?]/)[0].split('.').pop().trim()}`,"")
return client.sendFile(chatId, response.url, `file.${response.url.split(/[#?]/)[0].split('.').pop().trim()}`, response.title || "")
}
if(response.type=="custom"){
if(response["quick_replies"] && response["quick_replies"].length >= 1 && response["quick_replies"].length <= 3){
return client.sendButtons(message.from, response.wrapped.text , response["quick_replies"].map(qr=>{
return client.sendButtons(chatId, response.wrapped.text , response["quick_replies"].map(qr=>{
return {
id: qr.payload,
text: qr.title
Expand All @@ -207,6 +188,28 @@ export const setupBotPressHandler : (cliConfig : cliFlags, client: Client) => vo
} catch (error) {
console.error("BOTPRESS API ERROR", url, error.message)
}
}
client.onMessage(async message=>{
let text = message.body;
switch(message.type) {
case 'location':
text = `${message.lat},${message.lng}`;
break;
case 'buttons_response':
text = message.selectedButtonId;
break;
case 'document':
case 'image':
case 'audio':
case 'ptt':
case 'video':
if(message.cloudUrl) text = message.cloudUrl;
break;
default:
text = message.body;
break;
}
await sendBotPressMessage(text, message.from)
})
}

Expand Down

0 comments on commit 030b679

Please sign in to comment.