Collab Chat: Post

Basic Configs
Step Name
Note
Auto Step icon
Configs for this Auto Step
conf_ChannelName
C1: Channel Name to post to (Current case channel if blank)#{EL}
conf_MessageId
C2: Message ID to reply to (when posting a comment)
conf_Markdown
C3: Post Content (Markdown)#{EL}
conf_Files
C4: Data item that stores files to attach to the post
conf_PostId
C5: Data item to save the ID of the posted message/comment

Notes

  • When posting to an organization channel, app channel, or case channel, specify [C1: Channel Name to post to] as follows:
    • Organization channel: g{organization ID} (Example: g1)
    • App channel: m{app ID} (Example: m12)
    • Case channel: p{case ID} (Example: p2039)

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • feed-post.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step


const main = () => {
    ////// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
    const channelName = retrieveChannelName();
    const messageId = retrieveMessageId();
    const markdown = configs.get('conf_Markdown') ?? '';
    const files = retrieveFiles();

    if (markdown === '' && files.length === 0) {
        throw new Error('Post content and attached files are both empty.');
    }

    ////// == 演算 / Calculating ==
    const postId = post(channelName, messageId, markdown, files);

    ////// == ワークフローデータへの代入 / Data Updating ==
    saveData('conf_PostId', new java.math.BigDecimal(postId));
};

/**
 * config から投稿先チャンネル名を読み出す
 * 空の場合、この工程を処理しているケースのチャンネル名を返す
 * @returns {String}
 */
const retrieveChannelName = () => {
    const channelName = configs.get('conf_ChannelName') ?? '';
    if (channelName !== '') {
        return channelName;
    }
    return `p${processInstance.getProcessInstanceId()}`;
};

/**
 * config から投稿先メッセージ ID を読み出す
 * 未設定の場合、null を返す
 * @returns {Number|null}
 */
const retrieveMessageId = () => {
    const def = configs.getObject('conf_MessageId');
    if (def === null) {
        return null;
    }
    const messageId = engine.findData(def);
    if (messageId === null) {
        return null;
    }
    return messageId.longValue();
};

/**
 * config から添付ファイルを読み出す
 * 未設定の場合、空配列を返す
 * @returns {Array<QfileView>}
 */
const retrieveFiles = () => {
    const def = configs.getObject('conf_Files');
    if (def === null) {
        return [];
    }
    return engine.findData(def) ?? [];
};

/**
 * コラボチャットに投稿する
 * @param {String} channelName
 * @param {Number|null} messageId
 * @param {String} markdown
 * @param {Array} files
 * @returns {Number} 投稿したメッセージ/コメントの ID
 */
const post = (channelName, messageId, markdown, files) => {
    let feedWrapper = feedService.begin()
        .setChannel(channelName);
    if (messageId !== null) {
        feedWrapper = feedWrapper.setMessageId(messageId);
    }
    if (markdown !== '') {
        feedWrapper = feedWrapper.setMarkdown(markdown);
    }
    files.forEach(file => feedWrapper.attachFile(file));
    return feedWrapper.post();
};

/**
 * データ項目への保存
 * @param {String} configName
 * @param {*} data
 */
const saveData = (configName, data) => {
    const def = configs.getObject(configName);
    if (def === null) {
        return;
    }
    engine.setData(def, data);
};