Using a webhook

What I’m trying to do:
I am trying to complete a tutorial on implementing the Trello API and job application tutorial that is on Anvil’s website.

What I’ve tried and what’s not working:
Step 3 on this page is not making sense to me: Anvil | Adding automated emails using a Trello webhook

Where does the webhook code work? Right now I have it as below but I’m getting a syntax error:

Code Sample:

  def reject_applicant():
  # Trello sends two requests, first HEAD then POST. Checking for head request stops 505 on first request.
    if anvil.server.request.method == "HEAD":
      return {}
      
      # Get the old list ID or return a falsy object if an old list ID isn't found.
      previous_list_id = anvil.server.request.body_json.get('action', {}).get('data', {}).get('old', {}).get('idList') 
      # Check if card is being moved onto rejected list
      if previous_list_id and previous_list_id != REJECT_LIST_ID:
        card_id = anvil.server.request.body_json['action']['data']['card']['id'] 
        email_from_card = get_email_address_from_card(card_id)
        anvil.email.send(from_name="Derrick's Awesome Company Inc.", 
                  to=email_from_card, 
                  subject="Application for Player Support Manager at the Coolest Company Ever",
                  text="Sorry to say you aren't cool enough!")
        $ curl --request POST \
                --url 'https://api.trello.com/1/webhooks/?key=<API-KEY>&token=<API-TOKEN>&callbackURL=<APP-CALLBACK-URL>&idModel=<REJECT-LIST-ID>' \
                --header 'Accept: application/json' ``` 

It says to open the command line and to run it, but where do I do that as well? It’s assumes knowledge I’m unfamiliar with. Any help would be great. Thanks!

curl is a command that runs on your computer, has nothing to do with your app.

In this case you can use it to send an HTTP request to your app and test it.

If you are on Linux, it’s available out of the box. Just open a terminal window and run it there.

If you are on Windows… I do have it, but I don’t remember if it was available out of the box or if I installed it. Try, if it doesn’t work then you can try to google how to install curl on Windows.

1 Like

Thanks Stefano!

I have another issue I’m running into that I’m unsure what’s the cause. The error I’m seeing is:

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
at /lib/lib-python/3/json/decoder.py, line 357
  called from /lib/lib-python/3/json/decoder.py, line 339
  called from /lib/lib-python/3/json/__init__.py, line 361
  called from /site-packages/requests/models.py, line 889
  called from ServerModule1, line 41
  called from Form1, line 28

Here’s the code it’s referring to in ServerModule1:

  # Get the card ID and pass it to our new create attachment function
  new_card_id = response.json().get('id')
  create_card_attachment(new_card_id, resume)
  update_card_email_field(new_card_id, email)

I’m at a loss to what is causing this because it’s identical to the tutorial here: Anvil | Using the Trello API!

Any help would be appreciated.

I’m going to guess that JSONDecodeError: Expecting value means that the string you are trying to decode is not valid JSON or is missing.

Try adding a print(f"The response body was {response.get_bytes()}") before that line so you see what’s inside the response.

1 Like