Skip to content

Commit

Permalink
Merge pull request #16 from valgaze/send_files
Browse files Browse the repository at this point in the history
remove filesystem write and read for sendDataAsFile
  • Loading branch information
valgaze authored Nov 22, 2021
2 parents 3cf322e + 203b64b commit 3cf92d4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
21 changes: 13 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"license": "MIT",
"dependencies": {
"axios": "^0.24.0",
"form-data": "^4.0.0",
"simple-log-colors": "^1.1.0"
},
"peerDependencies": {
Expand Down
24 changes: 22 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'



Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -376,7 +381,22 @@ export class $Botutils {
}
}

public async sendDataAsFile<T=any>(data: T, extensionOrFileName: string, config: FileConfig ={}, fallbackText=' ') {
public async sendDataAsFile<T=any>(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<T=any>(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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3cf92d4

Please sign in to comment.