【问题标题】:Discord Webhook Linux Bash - How to pass content of file as string argumentDiscord Webhook Linux Bash - 如何将文件内容作为字符串参数传递
【发布时间】:2022-01-20 22:56:32
【问题描述】:

我正在尝试设置一个 Discord Webhook,用于在我的服务器上发布有关我的备份进程的信息。特别是作为嵌入 webhook 的 rsync 操作(大约 15 行摘要)的输出。

我决定使用 Discord.sh https://chaoticweg.cc/discord.sh/

这需要“line one\nnew line two\nthird line”的嵌入描述的字符串格式

由于 rsynclog 的输出跨越多行,我使用带有 jq 的脚本将其转换为这种格式。输入是:

Number of files: 101 (reg: 100, dir: 1)
Number of created files: 0
Number of deleted files: 0
Number of regular files transferred: 100
Total file size: 0 bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 0
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 5,247
Total bytes received: 1,916

sent 5,247 bytes  received 1,916 bytes  2,865.20 bytes/sec
total size is 0  speedup is 0.00

输出

"\nNumber of files: 101 (reg: 100, dir: 1)\nNumber of created files: 0\nNumber of deleted files: 0\nNumber of regular files transferred: 100\nTotal file size: 0 bytes\nTotal transferred file size: 0 bytes\nLiteral data: 0 bytes\nMatched data: 0 bytes\nFile list size: 0\nFile list generation time: 0.001 seconds\nFile list transfer time: 0.000 seconds\nTotal bytes sent: 5,247\nTotal bytes received: 1,916\n\nsent 5,247 bytes  received 1,916 bytes  2,865.20 bytes/sec\ntotal size is 0  speedup is 0.00\n"

我通过复制粘贴新文件的全部内容作为 discord.sh 嵌入描述的参数来验证这是否有效,如下所示:

discord.sh --webhook-url=URL --description="THE CONTENT OF THE TEMP.JSON FILE PASTED IN HERE"

这产生了预期的结果:

问题

我不知道如何将转换后的日志文件的内容作为参数传递给 discord.sh。我用直接复制粘贴验证了文件的格式是正确的,但我无法让它在 bash 中读取文件的内容。

我尝试了什么:

  • $(< /path/to/file)
  • $(cat /path/to/file)
  • ´/path/to/file´

我已经用尽了所有可以在网上找到的建议。它要么为包含无效 JSON 的消息返回错误,要么将文件内容的每个新单词视为新参数,并给我一个关于单词是无效参数的错误。即使文件清楚地包含引号,也应该将其视为一个字符串。

任何帮助将不胜感激

【问题讨论】:

    标签: json linux bash discord webhooks


    【解决方案1】:

    我认为 Bash 在您的代码中的某个位置删除了反斜杠。我建议您在尝试将变量传递给 webhook 机器人时,每一步都仔细检查变量的内容。

    我的主目录中有一个 test.txt:

    line1 bla bla bla\nline2 bla bla bla\nline3 bla bla bla\nline4 bla bla bla\nline5 last one

    然后我用 cat 读取该文件(注意文件是一行,因为行尾被替换为 \n)

    messagearray=($(cat test.txt))

    然后您可以将其传递给您的机器人

    sendcontent='{"username": "Yui", "content": "'"${messagearray[@]}"'"}'

    curl -H "Content-Type: application/json" -d "$sendcontent" "$webhookurl"

    这是经过测试的,可以与我的机器人一起使用。

    我不完全知道您的代码哪里出错了,但如果您按照我展示的方式尝试,我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多