OfficeScripts to send notification to teams chat

To send a notification to a Teams chat using OfficeScripts, you can make use of the Microsoft Teams API by sending an HTTP POST request. Here’s an example of a function that sends a notification to a Teams chat:

function sendTeamsNotification(message) {
  var url = "https://graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages";
  var requestOptions = {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      body: {
        content: message
      }
    })
  };

  return ExcelScript.UrlFetch.fetch(url, requestOptions)
    .then(function (response) {
      if (response.status === 201) {
        console.log("Notification sent successfully");
      } else {
        console.log("Error occurred while sending notification. Status: " + response.status);
      }
    })
    .catch(function (error) {
      console.log("Error occurred: " + error);
    });
}

In this example, the sendTeamsNotification function takes a message parameter, which represents the content of the notification you want to send. The function constructs the URL with the appropriate teamId and channelId for the Teams chat you want to send the notification to.

The function then sends an HTTP POST request to the Microsoft Teams API using ExcelScript.UrlFetch.fetch. The request includes the message content in the request body as JSON.

Once the response is received, the function checks the status code of the response. If the status code is 201 (indicating a successful response), it logs a success message. If the status code is not 201, it logs an error message with the status code.

To use this function, you need to replace {teamId} and {channelId} in the URL with the appropriate values for your Teams chat. For example:

sendTeamsNotification("Hello, this is a notification")
  .catch(function (error) {
    console.log("Error occurred: " + error);
  });

Please note that you need to have the necessary permissions and access to send notifications to the specified Teams chat. Additionally, you may need to authenticate and authorize your OfficeScripts to access the Microsoft Teams API.

Keep in mind that OfficeScripts are only supported in Excel for the web and Excel Online, and they may have limitations compared to other programming environments.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech