0
4

More than 3 years have passed since last update.

Python で UIAutomation を使って Slack に書き込んでみた

Posted at

Slack コネクトなメンバーを含んだDMチャンネル?に自動で定期的に書き込みをしたくて作ってみました。

Slack API は権限の関係なのかSlack コネクトな情報が取れなくて断念。。

何とか自動化を。。と考えて、UIAutomation が使えそう!!ってことで以下な感じのソースが出来上がりました。

import comtypes
from comtypes import CoCreateInstance

import comtypes.client
comtypes.client.GetModule('UIAutomationCore.dll')

from comtypes.gen.UIAutomationClient import *


def slack_send(message):
    try:
        #事前準備
        uia = CoCreateInstance(
            CUIAutomation._reg_clsid_, 
            interface=IUIAutomation, 
            clsctx=comtypes.CLSCTX_INPROC_SERVER|comtypes.CLSCTX_INPROC_HANDLER|comtypes.CLSCTX_LOCAL_SERVER|comtypes.CLSCTX_REMOTE_SERVER
        )
        root_element = uia.GetRootElement()

        # SlackWindow特定
        cndWindow = uia.CreatePropertyCondition(UIA_NamePropertyId, "Slack |***** | *****" )

        # SlackEditBox特定
        cnd1 = uia.CreatePropertyCondition(UIA_NamePropertyId, "*****にメッセージを送信する")
        cnd2 = uia.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_EditControlTypeId)
        cndEdit = uia.CreateAndCondition(cnd1, cnd2)
        Edit = root_element.FindFirst(TreeScope_Children, cndWindow).FindFirst(TreeScope_Subtree, cndEdit)

        # SlackEditBox へ 書き込み
        EditVal = Edit.GetCurrentPattern(UIA_ValuePatternId)
        EditVal.QueryInterface(IUIAutomationValuePattern).SetValue(message)

        # Slack送信ボタン特定
        cnd4 = uia.CreatePropertyCondition(UIA_NamePropertyId, "今すぐ送信する")
        cnd5 = uia.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_ButtonControlTypeId)
        cndBtn = uia.CreateAndCondition(cnd4, cnd5)
        Btn = uia.GetRootElement().FindFirst(TreeScope_Children, cndWindow).FindFirst(TreeScope_Subtree, cndBtn)

        # Slackメッセージ送信(送信ボタンクリック)
        BtnSend = Btn.GetCurrentPattern(UIA_InvokePatternId)
        BtnSend.QueryInterface(IUIAutomationInvokePattern).Invoke()

    except Exception as e:
        print(e)
        print("Slack へのメッセージ送信に失敗しました")


slack_send('test')

1.Slackのウィンドウ を特定して、
2.Slackウィンドウ内の書き込み領域 を特定して
3.書き込みして
4.送信ボタン を特定して
5.送信ボタンをクリック

するような動きになってます。
見つからなかった!等のエラー処理は入ってませんので様々なケースに柔軟に対応はできないですが、
とりあえず、開いておいた特定のDMに対する自動書き込みができたので、これでOK

こいつをWindowsのタスクスケジューラに設定して、定期的に実行するようにすれば自動書き込み完成です。

ソースの中の「*****」な部分については、Inspect.exe で調べてくださいデス。
こことか:https://github.com/blackrosezy/gui-inspect-tool
こことか:Qiita:Inspect.exe のインストール (Windows SDK)

「UIA_EditControlTypeId」やら「IUIAutomationInvokePattern」やらが、UIAutomation 使う上でのメンドクサイとこですが、
しっかり調べてキッチリ指定してやれば、意図通りに動くハズです。
やっぱり公式サイトが一番!
https://docs.microsoft.com/ja-jp/windows/win32/winauto/uiauto-controltype-ids
https://docs.microsoft.com/ja-jp/windows/win32/api/uiautomationclient/nn-uiautomationclient-iuiautomationinvokepattern

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4