code.py
 1  # SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  
 4  import os
 5  import ssl
 6  import wifi
 7  import socketpool
 8  import adafruit_requests
 9  
10  # add your mastodon token as 'mastodon_token' to your .env file
11  headers = {'Authorization': 'Bearer ' + os.getenv('mastodon_token')}
12  
13  # add your mastodon instance to your .env file as mastodon_host
14  url = 'https://' + os.getenv('mastodon_host') + '/api/v1/statuses'
15  
16  # connect to SSID
17  wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD'))
18  
19  pool = socketpool.SocketPool(wifi.radio)
20  requests = adafruit_requests.Session(pool, ssl.create_default_context())
21  
22  # you'll be prompted in the REPL to enter your post text
23  post = input("Please enter your Mastodon post text: ")
24  # pack the post for sending with the API
25  post_text = {"status": post}
26  
27  # confirm in the REPL that you want to post by entering y for yes or n for no
28  send_check = input("Send post to Mastodon (y/n)?")
29  
30  # if you type y
31  if send_check == "y":
32      # send to mastodon with a POST request
33      r = requests.post(url, data=post_text, headers=headers)
34      print()
35      print("You posted '%s' to Mastodon. Goodbye." % post)
36  # if you type n
37  else:
38      print("You did not post to Mastodon. Goodbye.")