GithubHelp home page GithubHelp logo

pibusylight's Introduction

Python Teams Presence Reader

language:Python license:MIT

This sample uses Microsoft Graph to read your user Teams presence status.

This is based on my PythonTeamsPresence

Authentication is handled via device flow authentication, the recommended approach for PiBusyLight. If you're looking for examples of how to work with Microsoft Graph from Python web applications, see Python authentication samples for Microsoft Graph.

Installation

Verify that you have the following prerequisites in place before installing the sample:

  • Install Python from https://www.python.org/. You'll need Python 3.6 or later, primarily because of the use of f-strings &mdash change those to format strings if you need to use an earlier Python 3.x version. If your code base is running under Python 2.7, you may find it helpful to use the 3to2 tools to port the code to Python 2.7.
  • The sample can be run on any operating system that supports Python 3.x, including recent versions of Windows, Linux, and Mac OS. In some cases, you may need to use a different command to launch Python — for example, some Linux distros reserve the command python for Python 2.7, so you need to use python3 to launch an installed Python 3.x version.
  • This sample requires an Office 365 for business account.
  • To register your application in the Azure Portal, you will need an Azure account associated with your Office 365 account. No charges will be incurred for registering your application in the Azure Portal, but you must have an account. If you don't have one, you can sign up for an Azure Free Account.

Follow these steps to install the sample code on your computer:

  1. Clone the repo with this command:

    • git clone https://github.com/poiriersimon/PiBusyLight.git
  2. Install Raspbian with Desktop on you Pi

  3. In the root folder of your cloned repo, install the dependencies for the sample as listed in the requirements.txt file with this command: pip install -r requirements.txt.

Application Registration if you don't want to use mine. (Optional)

To run the sample, you will need to register an application and add the registered application's ID to the configuration information in the config.py file. Follow these steps to register and configure your application:

  1. Navigate to the Azure portal > App registrations to register your app. Sign in using a work or school account, or a personal Microsoft account.

  2. Select New registration.

  3. When the Register an application page appears, set the values as follows:

    1. Set Name to PiBusyLight.
    2. Set Supported account types to Accounts in any organizational directory and personal Microsoft accounts.
    3. Leave Redirect URI empty.
    4. Choose Register.
  4. On the PiBusyLight overview page, copy and save the value for the Application (client) ID. You'll need it later.

  5. Select API permissions.

    1. Choose the Add a permission button and then make sure that the Microsoft APIs tab is selected.
    2. In the Commonly used Microsoft APIs section, select Microsoft Graph, and then select Delegated permissions.
    3. Use the Select permissions search box to search for the Presence.Read permission.
    4. Select the checkbox for each permission as it appears.

      NOTE: Permissions will not remain visible in the list as you select each one.

  6. Go to the Authentication page.

    1. Check the box next to https://login.microsoftonline.com/common/oauth2/nativeclient.
    2. Find the setting labeled Default client type and set it to Yes.
    3. Select Save at the top of the page.

After registering your application, modify the config.py file in the root folder of your cloned repo, and follow the instructions to enter your Client ID (the Application ID value you copied in Step 3 earlier). Save the change, and you're ready to run the sample.

Running the sample

Follow these steps to run the sample app:

  1. Go to Controlling Multiple LEDs With Python and Your Raspberry Pi's GPIO Pins and do the Step 1 and 2

  2. Modify the config.py to put the right GPIO code based on your setup

  3. At the command prompt, run the command python PiBusyLight.py. You'll see a message telling you to open a page in your browser and enter a code.

  4. After entering the code at https://aka.ms/devicelogin, you'll be prompted to select an identity or enter an email address to identify yourself. The identity you use must be in the same organization/tenant where the application was registered. Sign in, and then you'll be asked to consent to the application's delegated permissions as shown below. Choose Accept.

  5. After consenting to permissions, you'll see a message giving your Presence and the corresponding LED will light

To simplify the multiple run of the script, it store the Refresh Token in a file with the script, please threath this as a password.

Helper functions

Several helper functions in helpers.py provide simple wrappers for common Graph operations, and provide examples of how to make authenticated Graph requests via the methods of the session object. These helper functions can be used with any auth library — the only requirement is that the session object has a valid Graph access token stored in its Authorization header.

A note on HTTP headers

In this sample, the session object sends the required Authorization header (which contains the access token) as well as optional headers to identify the libraries used. These headers are set during the authentication process. In addition, you may want to create other headers for certain Graph calls. You can do this by passing a headers dictionary to the Graph call, and this dictionary will be merged with the default headers on the session object. You can see an example of this technique in parameter for any of the send_mail helper function, which adds a Content-Type header as shown here.

api_endpoint(url)

Converts a relative path such as /me/presence to a full URI based on the current RESOURCE and API_VERSION settings in config.py.

get_access_token(client_id)

Obtain the Access Token by leveraging the Refresh Token is present and valide, else leverage the device_flow_session to get a new one

led_all_off(off)

Turn all LED off, no need to pass argument

led_status(GPIO,status)

Send the True/False to the specified GPIO

initiate_led

If you never used your GPIO to control LED this will set them up.

Contributing

These samples are open source, released under the MIT License. Issues (including feature requests and/or questions about this sample) and pull requests are welcome. If there's another Python sample you'd like to see for Microsoft Graph, we're interested in that feedback as well — please log an issue and let us know!

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Resources

pibusylight's People

Contributors

poiriersimon avatar

Watchers

Kim Pihlström avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.