我安装了 gatsby-plugin-support-chat 并按照这里的教程进行操作(https://www.gatsbyjs.com/plugins/gatsby-plugin-support-chat)。
当我运行
npx ngrok http 8000
,并尝试在聊天机器人中发送消息时,会出现这样的500错误。
Module parse failed: Unexpected token (4:13)
You may need an appropriate loader to handle this file type, currently no
loaders are configured to process this file. See
https://webpack.js.org/concepts#loaders
| import { WebClient } from "@slack/web-api"
|
> let channelID: string = process.env.CHANNEL_ID
| let token: string = process.env.SLACK_TOKEN
当然,我在
env.development
文件中添加了2个以上环境变量,并使用组件文件中的控制台检查了它们。
此错误仅发生在 ode_modules\gatsby-plugin-support-chat\src pi\gatsby-plugin-support-chat\send-message.ts.
send-message.ts的内容是这样的
import { GatsbyFunctionRequest, GatsbyFunctionResponse } from "gatsby"
import { WebClient } from "@slack/web-api"
let channelID: string = process.env.CHANNEL_ID
let token: string = process.env.SLACK_TOKEN
const web = new WebClient(token)
interface SlackRequest {
channel: string
text: string
thread_ts?: string
}
export default async function handler(
req: GatsbyFunctionRequest,
res: GatsbyFunctionResponse
) {
if (req.method != "POST") {
return res.status(401).json({
message: "Use post method",
})
}
let requestData: SlackRequest
try {
if (req.body.thread_ts != null) {
requestData = {
channel: channelID,
text: req.body.message,
thread_ts: req.body.thread_ts,
}
} else {
requestData = { channel: channelID, text: req.body.message }
}
const result = await web.chat.postMessage(requestData).then(res => {
return res
})
res.json(result)
} catch (error) {
console.log(error)
res.status(500).send(error)
}
}
有人知道这个问题的解决方案吗? 谢谢