Microsoft Teams Chat Message Post

Microsoft Teams Chat メッセージ投稿

Posts a message to Microsoft Teams Chat using the API (Microsoft Graph REST API).

Auto Step icon
Configs for this Auto Step
conf_OAuth2
A: OAuth 2.0 Setting *
conf_chatId
B: Chat id *#{EL}
conf_subject
C: Title
conf_messsage
D: Message *
conf_messageId
O: Message Id (update)
conf_log
X: Access Log (update)
Script (click to open)


// Post Message Teams Chat via Microsoft Graph API v1.0 (ver. 202209)
// (c) 2022, Questetra, Inc. (the MIT License)

//// == 工程コンフィグの参照 / Config Retrieving ==
const oauth2  = configs.get( "conf_OAuth2" ) + "";

const chatId  = configs.get( "conf_chatId" ) + "";
let subject;
const dataId_subject = configs.get( "conf_subject" );
if ( dataId_subject !== "" ) {
  subject = engine.findDataByNumber( dataId_subject ) + "";
}
const content = engine.findDataByNumber( configs.get( "conf_messsage" ) ) + "";

const dataId_messageId = configs.get( "conf_messageId" );
const dataId_log       = configs.get( "conf_log" );


//// == 演算 / Calculating ==
let requestObj = {};
if ( subject ) {
  requestObj.subject = subject;
}
  requestObj.body = {};
  requestObj.body.contentType = "html";
  requestObj.body.content = content;

//engine.log("json:"+JSON.stringify( requestObj ));

let accessLog = "";
let uri = "https://graph.microsoft.com/v1.0/me/chats/" + chatId + "/messages";
let response = httpClient.begin()
  .authSetting( oauth2 )
  .body( JSON.stringify( requestObj ), "application/json" )
  .post( uri );
accessLog += "---POST request--- " + response.getStatusCode() + "\n";
accessLog += response.getResponseAsString() + "\n";


//// == ワークフローデータへの代入 / Data Updating ==
var status = response.getStatusCode();

if( status >= 300 ){
  engine.log(accessLog);
  throw `Failed in POST request. status: ${status}`;
} else {
  const jsonObj = JSON.parse( response.getResponseAsString() );
  let messageId = jsonObj.id + "";
  if( dataId_messageId !== "" ){
    engine.setDataByNumber( dataId_messageId, messageId );
  }
}

if( dataId_log !== "" ){
  engine.setDataByNumber( dataId_log, accessLog );
}

Download

warning Freely modifiable JavaScript (ECMAScript) code. No warranty of any kind.
(Installing Addon Auto-Steps are available only on the Professional edition.)

Notes

Capture

Appendix

You can embed a hyperlink to the process detail page by entering the following in ” Update Data” in preparation for the body of the submission message.
Link to Process

See Also

%d bloggers like this: