You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用AppleScript/JS实现Slack新私信自动化?无法获取发件人及回复

Slack网页版私信自动化:识别发件人、操作及回复方案(AppleScript+JavaScript)

由于公司禁用Slack API,只能通过Safari网页版结合AppleScript+JavaScript实现私信自动化。针对你现有的未读数量获取能力,以下是识别发件人、读取消息、自动回复的具体实现方案:

一、识别未读私信的发送人

通过定位带未读标记的会话项,提取其中的发送人名称:

tell application "Safari"
    set unreadSenders to do JavaScript "
        // 筛选所有带未读标记的私信会话
        const unreadConvs = document.querySelectorAll('.p-channel_sidebar__channel:has(.c-mention_badge)');
        const senders = [];
        unreadConvs.forEach(conv => {
            const nameEl = conv.querySelector('.p-channel_sidebar__name');
            if (nameEl) senders.push(nameEl.textContent.trim());
        });
        senders.join(', ');
    " in tab 2 of window 1
    display dialog "未读私信发送人:" & unreadSenders
end tell

注:Slack网页版的DOM类名可能随版本更新变化,可通过F12开发者工具查看当前页面的实际类名。

二、打开未读会话并读取消息内容

先点击未读会话加载消息,再提取最新消息的发送人和内容:

tell application "Safari"
    -- 点击第一个未读会话
    do JavaScript "document.querySelector('.p-channel_sidebar__channel:has(.c-mention_badge)').click();" in tab 2 of window 1
    delay 1 -- 等待消息加载,可根据网络情况调整时长
    
    -- 获取最新消息详情
    set latestMsg to do JavaScript "
        const lastMsgItem = document.querySelector('.c-message_list__item:last-child');
        if (!lastMsgItem) return '无消息内容';
        const sender = lastMsgItem.querySelector('.c-message__sender').textContent.trim();
        const content = lastMsgItem.querySelector('.c-message__body').textContent.trim();
        `发送人:${sender}\n消息内容:${content}`;
    " in tab 2 of window 1
    display dialog "最新私信:\n" & latestMsg
end tell

三、自动回复私信

定位回复输入框,填充内容并模拟发送操作:

tell application "Safari"
    set replyText to "您好,我当前无法及时回复,稍后会尽快联系您!"
    -- 填充回复内容
    do JavaScript `document.querySelector('.c-texty_input__textarea').value = '${replyText}';` in tab 2 of window 1
    
    -- 模拟回车发送(部分场景可能需要点击发送按钮)
    do JavaScript "document.querySelector('.c-texty_input__textarea').dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter', bubbles: true}));" in tab 2 of window 1
    
    -- 若回车发送无效,可替换为点击发送按钮:
    -- do JavaScript "document.querySelector('.c-texty_send_button').click();" in tab 2 of window 1
end tell

关键注意事项

  • 确保Safari已开启JavaScript功能,且Slack网页处于登录状态
  • DOM类名可能随Slack更新变化,需定期验证并调整代码中的选择器
  • 多未读会话场景下,可通过循环遍历unreadConvs实现批量处理

内容的提问来源于stack exchange,提问作者Kevin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 12:20:32