page_type | description | products | languages | extensions | urlFragment | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
This sample app demonstrates posting notifications for team/channel changes using Microsoft Graph API in Node.js |
|
|
|
officedev-microsoft-teams-samples-userscope-change-notification-web-app-nodejs |
This sample application uses Microsoft Graph API to notify users of team or channel changes, such as creation, editing, or deletion. It supports Graph API, MSAL authentication, and user-scope notifications in a Node.js web app.
- Graph API
- Web Application
- MSAL Authentication
- User-Scope Graph Notifications
- Microsoft Teams is installed and you have an account (not a guest account)
- NodeJS
- dev tunnel or ngrok latest version or equivalent tunneling solution
- M365 developer account or access to a Teams account with the appropriate permissions to install an app.
Note: Please create and install self-signed certificate before running this sample using toolkit or manually.
Note these instructions are for running the sample on your local machine.
-
Run ngrok - point to port 3978
ngrok http 3978 --host-header="localhost:3978"
Alternatively, you can also use the
dev tunnels
. Please follow Create and host a dev tunnel and host the tunnel with anonymous user access command as shown below:devtunnel host -p 3978 --allow-anonymous
-
App Registration
- Register a new application in the Microsoft Entra ID - App Registrations portal.
- On the overview page, copy and save the Application (client) ID, Directory (tenant) ID. You'll need those later when updating your Teams application manifest and in the .env file.
- Navigate to API Permissions, and make sure to add the follow permissions:
-
Select Add a permission
-
Select Microsoft Graph -> Application permissions.
-
Chat.ReadBasic
,Chat.Read
,Chat.ReadWrite
-
Click on Add permissions. Please make sure to grant the admin consent for the required permissions.
-
Navigate to the Certificates & secrets. In the Client secrets section, click on "+ New client secret". Add a description (Name of the secret) for the secret and select "Never" for Expires. Click "Add". Once the client secret is created, copy its value, it need to be placed in the appsettings.json file.
-
Navigate to the Authentication . Update Authentication Section by Adding Single-page application Redirect URI with your baseurl/ngrok. Single Page Application
To include resource data of graph notifications, this Graph API require self-signed certificate. Follow the below steps to create and manage certificate.
-
You can self-sign the certificate, since Microsoft Graph does not verify the certificate issuer, and uses the public key for only encryption.
-
Use Azure Key Vault as the solution to create, rotate, and securely manage certificates. Make sure the keys satisfy the following criteria:
- The key must be of type
RSA
- The key size must be between 2048 and 4096 bits
- The key must be of type
-
Follow this documentation for the steps - Create and install Self-Signed certificate
-
Clone the repository
git clone https://github.com/OfficeDev/Microsoft-Teams-Samples.git
A) If you are using Visual Studio Code
- Launch Visual Studio code
- File -> Open Folder
- Navigate to
samples/user-scope-web-application
folder. - Select
nodejs
folder.
Instruction for .env
1. Provide Update configuration with the MicrosoftAppId
, MicrosoftAppPassword
, MicrosoftAppTenantId
,
Base64EncodedCertificate
, EncryptionCertificateId
, PRIVATE_KEY_PATH
in the .env that is created in Azure.
2. Provide the tunnel url as "BaseUrl" in appsetting on which application is running on.
3. You should be having Base64EncodedCertificate from Create and install Self-Signed certificate step.
4. Use Certificate "PEM" format and add the certificate name for PRIVATE_KEY_PATH
For eg PRIVATE_KEY_PATH
=PrivateKeyFileName.pem" in .env file. Also make sure the private key file is stored inside helper folder of this project.
Note : notificationUrl
will be updated automatically from teamsapp.local.yml
file when you run application by teams toolkit. And when you run locally through npm start
then your notificationUrl will be like : https://1234.ngrok-free.app/api/notifications
and if you are using dev tunnels, your notificationUrl will be like: https://12345.devtunnels.ms/api/notifications
.
Update For MicrosoftftAppId In sample code
1. Navigate to samples/user-scope-web-application/client/src/components/login.jsx
file and update <<Microsoft-App-Id>>
with your MicrosoftAppID at line number 20.
2. Navigate to samples/user-scope-web-application/client/src/components/userScopeTestApp.jsx
file and update <<Microsoft-App-Id>>
with your MicrosoftAppID at line number 31.
-
Install node modules
Inside node js folder, open your local terminal and run the below command to install node modules. You can do the same for client folder by opening the project in Visual Studio code.
npm install
Run your app
npm start
You can interact with user scope web application by logging with demo tenant.