如何根据事件 Google PubSub 发送消息?

How to send a message based on event Google PubSub?

我需要一些关于 PubSub 的帮助...

每当有人在我的网站上接受 cookie 时,我都需要向主题发送消息。此消息应包含已接受服务的编码 URL。

我有这个脚本:

const topicName = "myTopic";
const data = JSON.stringify({
  encodeConsentURL:""
});

// Imports the Google Cloud client library
const { PubSub } = require("@google-cloud/pubsub");

// Creates a client; cache this for further use
const pubSubClient = new PubSub();

async function publishMessageWithCustomAttributes() {
  // Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
  const dataBuffer = Buffer.from(data);

  // Add two custom attributes, origin and username, to the message
  const customAttributes = {};

  const messageId = await pubSubClient
    .topic(topicName)
    .publish(dataBuffer, customAttributes);
  console.log(`Message ${messageId} published.`);
  console.log(customAttributes);
}

publishMessageWithCustomAttributes().catch(console.error);

这段代码有效,它发送消息,我发现很难做的是如何在我的 cookie 脚本中为 运行 这段代码设置正确的一切。在我的 cookie 脚本中,我有一个写入 cookie 的函数,在我想发送消息的同一函数中,这可能吗?提前致谢!

有点晚了,但我不明白,如果您已经有一个有效的 cookie 脚本和一个有效的发布脚本,难道不只是将它们放在一起吗? 如果您仍然需要帮助,我很乐意为您提供帮助

类似

const runEverything = async () => {
  try {
    await checkCookiesThing()
    await publishMessage()

  } catch (e) {
    console.error(e)
  }
}