updates
This commit is contained in:
1 parent
414a9ff5ee
commit
902cd59d11
1 file changed
+51
-30
@@ -1,10 +1,12 @@
|
|||||||
|
import 'dotenv/config'
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import 'dotenv/config'
|
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
app.use(express.json({ limit: '10mb' }))
|
app.use(express.urlencoded({ extended: false }))
|
||||||
|
app.use(express.json())
|
||||||
|
app.use(express.text({ type: '*/*' }))
|
||||||
|
|
||||||
const PORT = process.env.PORT || 3000
|
const PORT = process.env.PORT || 3000
|
||||||
|
|
||||||
@@ -17,51 +19,70 @@ app.post('/webhook', async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
console.log('Received webhook')
|
console.log('Received webhook')
|
||||||
|
|
||||||
console.log(JSON.stringify(req.body, null, 2))
|
console.log('HEADERS:', req.headers)
|
||||||
|
console.log('RAW BODY:', req.body)
|
||||||
|
|
||||||
// Try a few possible fields from AudioPen
|
let data = req.body
|
||||||
const note =
|
|
||||||
req.body.note ||
|
|
||||||
req.body.text ||
|
|
||||||
req.body.content ||
|
|
||||||
JSON.stringify(req.body, null, 2)
|
|
||||||
|
|
||||||
const timestamp = new Date()
|
// If body comes in as raw string (VERY likely here)
|
||||||
.toISOString()
|
if (typeof data === 'string') {
|
||||||
.replace(/:/g, '-')
|
try {
|
||||||
|
// try parsing as URL encoded manually
|
||||||
|
const params = new URLSearchParams(data)
|
||||||
|
|
||||||
const filename = `note-${timestamp}.md`
|
data = Object.fromEntries(params.entries())
|
||||||
|
} catch (e) {
|
||||||
|
console.log('Failed to parse raw body')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('PARSED DATA:', data)
|
||||||
|
|
||||||
|
const title = data.title || 'Untitled Note'
|
||||||
|
const body = data.body || ''
|
||||||
|
const transcript = data.orig_transcript || ''
|
||||||
|
|
||||||
|
const safeTitle = title
|
||||||
|
.replace(/[^a-z0-9]/gi, '-')
|
||||||
|
.replace(/-+/g, '-')
|
||||||
|
.toLowerCase()
|
||||||
|
|
||||||
|
const timestamp = new Date().toISOString().replace(/:/g, '-')
|
||||||
|
|
||||||
|
const filename = `${timestamp}-${safeTitle}.md`
|
||||||
|
|
||||||
|
const markdown = `# ${title}
|
||||||
|
|
||||||
|
${body}
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Original Transcript
|
||||||
|
|
||||||
|
${transcript}
|
||||||
|
`
|
||||||
|
|
||||||
const webdavUrl =
|
const webdavUrl =
|
||||||
`${NEXTCLOUD_URL}` +
|
`${process.env.NEXTCLOUD_URL}` +
|
||||||
`/remote.php/dav/files/` +
|
`/remote.php/dav/files/` +
|
||||||
`${NEXTCLOUD_USERNAME}/` +
|
`${process.env.NEXTCLOUD_USERNAME}/` +
|
||||||
`${NEXTCLOUD_FOLDER}/` +
|
`${process.env.NEXTCLOUD_FOLDER}/` +
|
||||||
`${filename}`
|
`${filename}`
|
||||||
|
|
||||||
await axios.put(webdavUrl, note, {
|
await axios.put(webdavUrl, markdown, {
|
||||||
auth: {
|
auth: {
|
||||||
username: NEXTCLOUD_USERNAME,
|
username: process.env.NEXTCLOUD_USERNAME,
|
||||||
password: NEXTCLOUD_PASSWORD
|
password: process.env.NEXTCLOUD_PASSWORD
|
||||||
},
|
},
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'text/markdown'
|
'Content-Type': 'text/markdown'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log(`Uploaded ${filename}`)
|
res.json({ success: true, filename })
|
||||||
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
filename
|
|
||||||
})
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err.response?.data || err.message)
|
console.error(err.response?.data || err.message)
|
||||||
|
res.status(500).json({ error: err.message })
|
||||||
res.status(500).json({
|
|
||||||
success: false,
|
|
||||||
error: err.message
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user