0

I have a GroupMe bot that can send messages to the chat it is assigned to in this format:

curl -d '{"text" : "text", "bot_id" : "(REDACTED)"}' https://api.groupme.com/v3/bots/post

So, instead of typing up this monstrosity every time I wanted to send a message, I decided to create an alias for it.

Here is what I attempted to use in my .bash_profile for this:

alias groupmessage=groupmessagefunction
groupmessagefunction() { curl -d '{"text" : $1, "bot_id" : "(REDACTED)"}' https://api.groupme.com/v3/bots/post; }

Could anyone inform me what the correct formatting would be to get this to work? Thanks!

Update 1:

I now have my code as follows:

v="bot_id_string"
alias groupmessage=groupmessagefunction
groupmessagefunction() { curl -d '{"text" : '"$1"', "bot_id" : '"$v"'}' https://api.groupme.com/v3/bots/post; }

I would like to point out that what I am trying to accomplish is type in something like:

groupmessage "hello"

or

groupmessage hello

Then, it will send out the following command:

curl -d '{"text" : "hello", "bot_id" : "bot_id_string"}' https://api.groupme.com/v3/bots/post

I hope this helps clarify this question if any misunderstanding occurred.

Gigi Bayte 2
  • 838
  • 1
  • 8
  • 20

1 Answers1

1

Now, I hope this will be the solution for you:

v=bot_id
curl -d '{"text" : "Test", "'$v'" : "(REDACTED)"}' api.groupme.com/v3/bots/post

You have to use ' ' around $v --> '$v' because this will allow bash to evaluate $v inside curl -d ' ... ' .

Answer for your Update 1

As I see it would be the better way to use eval command:

v="bot_id_string"
alias groupmessage=groupmessagefunction
groupmessagefunction() {
    a="curl -d '{\"text\" : \"$1\", \"bot_id\" :  \"$v\"}' https://api.groupme.com/v3/bots/post"
    echo "$a"    # this echo just for checking
    eval "$a"; }

groupmessage "Hello is it working now ?"
Gigi Bayte 2
  • 838
  • 1
  • 8
  • 20
  • Hello. Thank you for responding. When I run the program, I get: {"meta":{"code":400,"errors":["Invalid bot_id"]},"response":null}~ – Gigi Bayte 2 Nov 08 '16 at 14:31
  • However, curl -d '{"text" : "Test", "bot_id" : "(REDACTED)"}' https://api.groupme.com/v3/bots/post works when I type it on the command line, so I do not know what the problem is. – Gigi Bayte 2 Nov 08 '16 at 14:32
  • I have updated my question to clarify it if that helps. The newest solution still does not work, and the same error message occurs. – Gigi Bayte 2 Nov 09 '16 at 02:41
  • Thanks for your response. The same error occurred, and the echo produced curl -d '{text : "Test 2", bot_id : "eda9e33e92c82775b0a706321f"}' https://api.groupme.com/v3/bots/post – Gigi Bayte 2 Nov 10 '16 at 13:58
  • The echo doesn't have quotes around the words "text" and "bot_id" for some reason, though. – Gigi Bayte 2 Nov 10 '16 at 14:00