please help me what am I doing wrong? Im doing telegram notifier via ruby in my Fastfile and gets that problem:
[!] Syntax error in your Fastfile on line 175: Fastfile:175: syntax error, unexpected tIDENTIFIER, expecting end
210...ication/json' --data '{"chat_id": "123456789", "media": [{"t...
211... ^~~~~~~
212Fastfile:175: syntax error, unexpected tINTEGER, expecting end
213... --data '{"chat_id": "123456789", "media": [{"type": "photo"...
214... ^~~~~~~~~
My code:
lane :detox do
images = Dir.glob("folder/*.png")
images.each do |image|
puts image
sh "curl --request POST --header 'Content-Type: application/json' --data '{"chat_id": "123456789", "media": [{"type": "photo", "media": "attach://image"}]' https://api.telegram.org/botToken/sendMediaGroup"
end
CodePudding user response:
I see a few things wrong here. Firstly, you should escape all your quotation marks in the string following sh
. Otherwise, the string is terminated early and you'll encounter errors.
Secondly, you opened 2 do
blocks: lane :detox do
and images.each do |image|
, but you only have a single end
.
This should be
lane :detox do
images = Dir.glob("folder/*.png")
images.each do |image|
puts image
sh "curl --request POST --header 'Content-Type: application/json' --data '{\"chat_id\": \"123456789\", \"media\": [{\"type\": \"photo\", \"media\": \"attach://image\"}]}' https://api.telegram.org/botToken/sendMediaGroup"
end
end
CodePudding user response:
The syntax error is caused by using unescaped double quotes within a double quoted string:
str = "foo "bar" baz"
# ^^^^^ won't work
You can escape "
as \"
but it's often easier to use a heredoc instead which doesn't require escaping:
sh <<-CMD
curl https://api.telegram.org/botToken/sendMediaGroup \
--request POST \
--header 'Content-Type: application/json' \
--data '{"chat_id": "123456789", "media": [{"type": "photo", "media": "attach://image"}]}'
CMD
However, instead of building the command yourself, you can also pass the command options as separate arguments to sh
:
sh(
'curl', 'https://api.telegram.org/botToken/sendMediaGroup',
'--request', 'POST',
'--header', 'Content-Type: application/json',
'--data', '{"chat_id": "123456789", "media": [{"type": "photo", "media": "attach://image"}]}'
)
Which also allows you to create the JSON from a Ruby hash on the fly:
require 'json'
data = {
chat_id: "123456789",
media: [{ type: "photo", media: "attach://image" }]
}
sh(
'curl', 'https://api.telegram.org/botToken/sendMediaGroup',
'--request', 'POST',
'--header', 'Content-Type: application/json',
'--data', data.to_json
)
Doing so ensures that your JSON is always valid. (syntactically)