简体   繁体   English

Discord Webhook Linux Bash - 如何将文件内容作为字符串参数传递

[英]Discord Webhook Linux Bash - How to pass content of file as string argument

I'm trying to set up a Discord webhook that posts info about my backup processes on my server.我正在尝试设置一个 Discord webhook,它会在我的服务器上发布有关我的备份进程的信息。 Specifically the output of an rsync operation (about 15 lines of summary) as an embed webhook.特别是一个rsync操作的output(大概15行总结)作为一个embed webhook。

I decided to use Discord.sh https://chaoticweg.cc/discord.sh/我决定使用 Discord.sh https://chaoticweg.cc/discord.sh/

This expects a string format for the embed description of "line one\nnew line two\nthird line"这需要“line one\nnew line two\nthird line”的嵌入描述的字符串格式

Since the output of an rsynclog stretches over multiple lines I used a script with jq to convert it to this format.由于 rsynclog 的 output 跨越多行,我使用带有 jq 的脚本将其转换为这种格式。 The input was:输入是:

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

The output output

"\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"

I verified that this was working by copy pasting the entire content of the new file as the argument to the discord.sh embed description like this:我通过复制粘贴新文件的全部内容作为 discord.sh 嵌入描述的参数来验证这是否有效,如下所示:

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

This produced the expected result:这产生了预期的结果:

在此处输入图像描述

The Problem问题

I cannot figure out how to pass the content of the converted log file as an argument to discord.sh.我不知道如何将转换后的日志文件的内容作为参数传递给 discord.sh。 I verified the format of the file is correct with the straight copy paste but I cannot get it to read the content of the file in bash.我用直接复制粘贴验证了文件的格式是正确的,但我无法让它读取 bash 中的文件内容。

What I tried:我尝试了什么:

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

I've exhausted all suggestions I could find online.我已经用尽了所有可以在网上找到的建议。 It either returns an error for message containing invalid JSON or it treats every new word of the file content as a new argument and gives me an error about a word being an invalid argument.它要么为包含无效 JSON 的消息返回错误,要么将文件内容的每个新单词视为新参数,并给我一个关于单词是无效参数的错误。 Even though the file clearly contains quotes and it should treat it as one string.即使文件清楚地包含引号,它也应该将其视为一个字符串。

Any help would be greatly appreciated任何帮助将不胜感激

I think Bash removes the backslashes at some point in your code.我认为 Bash 在代码中的某个位置删除了反斜杠。 I advise that you double check the contents of your variables each step as you try to pass it to the webhook bot.我建议您在尝试将变量传递给 webhook 机器人时,每一步都仔细检查变量的内容。

I have a test.txt in my home dir:我的主目录中有一个 test.txt:

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

then I read that file with cat (note that the file is one line as the end of line is replaced with \n)然后我用 cat 读取该文件(请注意,该文件是一行,因为行尾被替换为 \n)

messagearray=($(cat test.txt))

you can then pass this to your bot然后您可以将其传递给您的机器人

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

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

This is tested and works with my bot.这是经过测试的,可以与我的机器人一起使用。

I don't exactly know where your code went wrong, but I hope this helps if you try it the way I showed.我不完全知道您的代码哪里出错了,但是如果您按照我展示的方式尝试,我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM