Start: Microsoft 365 Outlook for Business: Email Received

Start: Microsoft 365 Outlook for Business: Email Received

開始: Microsoft 365 Outlook for Business: メール受信時

This item starts a case when Outlook has received a new message.

Auto Step icon
Basic Configs
Step Name
Note
Configs for this Auto Step
conf_OAuth2
C1: OAuth2 Setting *
conf_Category
C2: Category *
conf_idData
C3: Data item to save Message ID *
conf_timestampData
C4: Data item to save received time
conf_rfc822msgIdData
C5: Data item to save RFC 822 Message-ID header

Notes

  • This modeling element is for Outlook for Business of Microsoft 365
    • It does not work for Outlook personal
  • Both default categories and user-created categories are supported
    • You can create a rule to add a category to new received messages automatically
  • Questetra BPM Suite will periodically poll Outlook to check for new received messages
    • If any new messages have been received, a Case will be started
  • A Case will not be started on the first check; only the check will be done
    • You can confirm the check status from the Case Log
  • If a large number of messages are received in a short period of time, the Cases may not be started for all messages
    • Approx. 90 Cases every 15 minutes

See Also

Script (click to open)
  • An XML file that contains the code below is available to download
    • outlook-message-received.xml (C) Questetra, Inc. (MIT License)
    • Just use it for a reference for the codes
    • This file cannot be imported into a Workflow App as an Add-on

// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://login.microsoftonline.com/{your-tenant-id}/oauth2/v2.0/authorize
// - Token Endpoint URL: https://login.microsoftonline.com/{your-tenant-id}/oauth2/v2.0/token
// - Scope: https://graph.microsoft.com/Mail.Read offline_access
// - Consumer Key: (Get by Microsoft Azure Active Directory)
// - Consumer Secret: (Get by Microsoft Azure Active Directory)

const DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssX";
const OUTLOOK_MESSAGE_LIST_LIMIT = 1000; // 取得上限数

/**
 * configs から必要な情報の取り出し
 * @returns {Object} setting 設定
 * @returns {AuthSettingWrapper} setting.auth HTTP 認証設定
 * @returns {String} setting.category カテゴリ名
 */
const prepare = () => {
    const auth = configs.getObject('conf_OAuth2');
    const category = configs.get('conf_Category');
    return {
        auth,
        category
    };
};

/**
 * 追加されたメールの検索
 * @param {Number} limit 取得上限数
 * @param timestampLowerLimit timestamp の下限
 * @returns {Array} messages メッセージ一覧
 * @returns {string} messages[].id Message ID
 * @returns {timestamp} messages[].timestamp メール受信時刻
 * @returns {string} messages[].rfc822msgId RFC 822 Message-ID
 */
const list = (limit, timestampLowerLimit) => {
    const {auth, category} = prepare();
    const messages = getMessages(auth, category, limit, timestampLowerLimit);
    logMessages(messages);
    return messages;
};

/**
 * メッセージ一覧のログ出力
 * @param {Array} messages メッセージ一覧
 */
const logMessages = (messages) => {
    const replacer = (key, value) => value instanceof java.sql.Timestamp ? dateFormatter.format(DATETIME_FORMAT, value) : value;
    messages.forEach(msg => engine.log(JSON.stringify(msg, replacer)));
};

/**
 * Microsoft Graph API にメール一覧取得の GET リクエストを送信し、メッセージ一覧を返す
 * @param {String} auth 認証設定
 * @param {String} category カテゴリ名
 * @param {Number} limit 取得上限数
 * @param timestampLowerLimit timestamp の下限
 * @returns {Array} messages メッセージ一覧
 * @returns {string} messages[].id メール ID
 * @returns {timestamp} messages[].timestamp メール受信時刻
 * @returns {string} messages[].rfc822msgId RFC 822 Message-ID
 */
function getMessages(auth, category, limit, timestampLowerLimit) {
    limit = Math.min(limit, OUTLOOK_MESSAGE_LIST_LIMIT); // limit のほうが小さくなるはずだが、念のため
    const query = `receivedDateTime ge ${dateFormatter.format('UTC', DATETIME_FORMAT, timestampLowerLimit)} and categories/any(c:c eq '${category}')`;
    const response = httpClient.begin().authSetting(auth)
        .queryParam('$top', `${limit}`)
        .queryParam('$select', 'id,receivedDateTime,internetMessageId')
        .queryParam('$orderby', 'receivedDateTime DESC')
        .queryParam('$filter', query)
        .get('https://graph.microsoft.com/v1.0/me/messages');
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw `Failed to get messages. status: ${status}`;
    }

    const messages = JSON.parse(responseStr).value;
    if (messages.length === 0) {
        // 該当メッセージが無い
        engine.log('no messages');
        return [];
    }

    return messages
        .filter(msg => !engine.isProcessStarted(msg.id)) // 既にプロセス開始済みのものを除く
        .map(msg => {
            return {
                id: msg.id,
                timestamp: dateFormatter.parse(DATETIME_FORMAT, msg.receivedDateTime),
                rfc822msgId: msg.internetMessageId
            };
        });
}


    
Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading