diff --git a/package-lock.json b/package-lock.json index 9af45a2..5306068 100644 --- a/package-lock.json +++ b/package-lock.json @@ -909,8 +909,7 @@ "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", - "dev": true + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, "at-least-node": { "version": "1.0.0", @@ -1247,7 +1246,6 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "requires": { "delayed-stream": "~1.0.0" } @@ -1406,8 +1404,7 @@ "delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", - "dev": true + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, "denque": { "version": "1.5.1", @@ -1706,6 +1703,16 @@ "integrity": "sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=", "dev": true }, + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, "fs-extra": { "version": "9.1.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.1.0.tgz", @@ -2683,14 +2690,12 @@ "mime-db": { "version": "1.49.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.49.0.tgz", - "integrity": "sha512-CIc8j9URtOVApSFCQIF+VBkX1RwXp/oMMOrqdyXSBXq5RWNEsRfyj1kiRnQgmNXmHxPoFIxOroKA3zcU9P+nAA==", - "dev": true + "integrity": "sha512-CIc8j9URtOVApSFCQIF+VBkX1RwXp/oMMOrqdyXSBXq5RWNEsRfyj1kiRnQgmNXmHxPoFIxOroKA3zcU9P+nAA==" }, "mime-types": { "version": "2.1.32", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.32.tgz", "integrity": "sha512-hJGaVS4G4c9TSMYh2n6SQAGrC4RnfU+daP8G7cSCmaqNjiOoUY0VHCMS42pxnQmVF1GWwFhbHWn3RIxCqTmZ9A==", - "dev": true, "requires": { "mime-db": "1.49.0" } diff --git a/package.json b/package.json index 499141d..e55f995 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "license": "MIT", "dependencies": { "axios": "^0.24.0", + "form-data": "^4.0.0", "simple-log-colors": "^1.1.0" }, "peerDependencies": { diff --git a/src/helpers.ts b/src/helpers.ts index 247e9f6..65763cb 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -4,6 +4,7 @@ import { SpeedyCard, chipLabel, chipConfigLabel } from './index' import { BotInst, Trigger, ToMessage, Message } from './framework' import { log, loud } from './logger' import { resolve } from 'path' +import FormData from 'form-data' @@ -213,6 +214,10 @@ export class $Botutils { public ContextKey = '_context_' // https://developer.webex.com/docs/basics public supportedExtensions = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'jpg', 'jpeg', 'bmp', 'gif', 'png'] + + private API = { + messages: 'https://webexapis.com/v1/messages', + } constructor(botRef: BotInst | any){ this.botRef = botRef this.token = botRef.framework.options.token @@ -376,7 +381,22 @@ export class $Botutils { } } - public async sendDataAsFile(data: T, extensionOrFileName: string, config: FileConfig ={}, fallbackText=' ') { + public async sendDataAsFile(data: T, extensionOrFileName: string, fallbackText=' ') { + const fullFileName = this.handleExt(extensionOrFileName) + + const formData = new FormData(); + formData.append('files', data, fullFileName) + formData.append('roomId', this.botRef.room.id) + formData.append('text', fallbackText) + const formDataHeaders = formData.getHeaders() + const headers = { + ...formDataHeaders, + Authorization: `Bearer ${this.token}`, + } + return axios.post(this.API.messages, formData, { headers }) + } + + public async _FSsendDataAsFile(data: T, extensionOrFileName: string, config: FileConfig ={}, fallbackText=' ') { // 🦆: HACK HACK HACK for "files": https://developer.webex.com/docs/basics // todo: get rid of filesystem write const fullFileName = this.handleExt(extensionOrFileName) @@ -426,7 +446,7 @@ export class $Botutils { const [prefix, ext] = input.split('.') if (hasDot) { - if (!prefix) { + if (!prefix || prefix === '*') { // '.json' case, generate prefix fileName = `${this.generateFileName()}.${ext}` } else {