Error: SyntaxError: Unexpected token C in JSON at position 33 in rule attachments while doing on google firebase
Error: SyntaxError: Unexpected token C in JSON at position 33 in rule attachments while doing on google firebase
我正在使用 GCP 和 slack 开发 Firebase。在这个集成过程中,我得到了这个
错误:语法错误:JSON 规则附件中第 33 位的意外标记 C .
我试了很多,可惜没找到解决方法。
实际错误仅在代码中..
任何人都可以帮我解决这个问题!!
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { WebClient } = require("@slack/web-api");
admin.initializeApp(functions.config().firebase);
const web = new WebClient(functions.config().gslack.api_token);
const runtimeOpts = {
timeoutSeconds: 30,
memory: "256MB"
};
const rules = [];
admin
.firestore()
.collection("gSlack")
.where("disabled", "==", false)
.onSnapshot(querySnapshot => {
querySnapshot.docChanges().forEach(change => {
const rule = {
id: change.doc.id,
...change.doc.data()
};
if (change.type === "added") {
rules.splice(change.newIndex, 0, rule);
}
if (change.type === "modified") {
rules.splice(change.newIndex, 1, rule);
}
if (change.type === "removed") {
rules.splice(change.oldIndex, 1);
}
});
});
function evalTest(data, test) {
try {
$ = data;
return eval(`'use strict';(${test});`);
} catch (err) {
console.error(err, test);
return false;
}
}
function evalMessageText(data, message) {
if (message) {
try {
$ = data;
return eval(`'use strict';\`${message}\`;`);
} catch (err) {
console.error(err, message);
return `Error: ${err} in rule message:\n ${message}`;
}
}
}
function evalMessageAttachments(data, attachments) {
if (attachments) {
try {
$ = data;
return JSON.parse(
eval(`'use strict';\`${JSON.stringify(attachments)}\`;`)
);
} catch (err) {
console.error(err, attachments);
return [
{
title: "GSLACK ERROR",
text: `Error: ${err} in rule attachments`
}
];
}
}
}
function sendSlack(data, rule) {
return web.chat.postMessage({
as_user: true,
channel: rule.channel,
text: evalMessageText(data, rule.message),
attachments: evalMessageAttachments(data, rule.attachments)
});
}
exports.gSlack = functions
.runWith(runtimeOpts)
.pubsub.topic("gslack")
.onPublish((message, context) => {
const data = message.json;
return Promise.all(
rules.map(rule => {
if (evalTest(data, rule.test)) {
return sendSlack(data, rule);
}
return Promise.resolve();
})
);
});
上面是我的代码1.下面是json文件的另一个代码
{
"name": "gslack",
"description": "",
"engines": {
"node": "10"
},
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions:gSlack",
"logs": "firebase functions:log"
},
"dependencies": {
"@slack/web-api": "^5.0.1",
"firebase-admin": "~9.2.0",
"firebase-functions": "^3.11.0"
},
"private": true
}
以上代码没有错误。 Firebase 控制台上的数据应该是清楚的。
我正在使用 GCP 和 slack 开发 Firebase。在这个集成过程中,我得到了这个
错误:语法错误:JSON 规则附件中第 33 位的意外标记 C .
我试了很多,可惜没找到解决方法。
实际错误仅在代码中..
任何人都可以帮我解决这个问题!!
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { WebClient } = require("@slack/web-api");
admin.initializeApp(functions.config().firebase);
const web = new WebClient(functions.config().gslack.api_token);
const runtimeOpts = {
timeoutSeconds: 30,
memory: "256MB"
};
const rules = [];
admin
.firestore()
.collection("gSlack")
.where("disabled", "==", false)
.onSnapshot(querySnapshot => {
querySnapshot.docChanges().forEach(change => {
const rule = {
id: change.doc.id,
...change.doc.data()
};
if (change.type === "added") {
rules.splice(change.newIndex, 0, rule);
}
if (change.type === "modified") {
rules.splice(change.newIndex, 1, rule);
}
if (change.type === "removed") {
rules.splice(change.oldIndex, 1);
}
});
});
function evalTest(data, test) {
try {
$ = data;
return eval(`'use strict';(${test});`);
} catch (err) {
console.error(err, test);
return false;
}
}
function evalMessageText(data, message) {
if (message) {
try {
$ = data;
return eval(`'use strict';\`${message}\`;`);
} catch (err) {
console.error(err, message);
return `Error: ${err} in rule message:\n ${message}`;
}
}
}
function evalMessageAttachments(data, attachments) {
if (attachments) {
try {
$ = data;
return JSON.parse(
eval(`'use strict';\`${JSON.stringify(attachments)}\`;`)
);
} catch (err) {
console.error(err, attachments);
return [
{
title: "GSLACK ERROR",
text: `Error: ${err} in rule attachments`
}
];
}
}
}
function sendSlack(data, rule) {
return web.chat.postMessage({
as_user: true,
channel: rule.channel,
text: evalMessageText(data, rule.message),
attachments: evalMessageAttachments(data, rule.attachments)
});
}
exports.gSlack = functions
.runWith(runtimeOpts)
.pubsub.topic("gslack")
.onPublish((message, context) => {
const data = message.json;
return Promise.all(
rules.map(rule => {
if (evalTest(data, rule.test)) {
return sendSlack(data, rule);
}
return Promise.resolve();
})
);
});
上面是我的代码1.下面是json文件的另一个代码
{
"name": "gslack",
"description": "",
"engines": {
"node": "10"
},
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions:gSlack",
"logs": "firebase functions:log"
},
"dependencies": {
"@slack/web-api": "^5.0.1",
"firebase-admin": "~9.2.0",
"firebase-functions": "^3.11.0"
},
"private": true
}
以上代码没有错误。 Firebase 控制台上的数据应该是清楚的。