WindowsAPI自动发送消息——不说不说messagemessage开始吧

[复制链接]
查看1152 | 回复0 | 2022-6-8 09:14:17 | 显示全部楼层 |阅读模式 来自 中国
博客介绍

前言:

嗯,我不知道组里的朋友为什么会这样。 开始抢龙王? ? 这个水龙头是干什么用的?这群人可能没时间了。嗯,我也是,不过一个个发消息真的很累,于是想到了用python自动发消息——废话不多说,开始吧,龙王就是我。

安装pywin32库

pywin32是一个Python库,为python提供了访问Windows API的扩展,提供了一套完整的windows常量、接口、线程和COM机制……我们这里的QQ消息发送使用了窗口遍历、消息发送、剪贴板以及pywin32下的其他API。正式使用前,pip install:

定义消息发送类

这个类要实现的功能是将消息复制到剪贴板,然后遍历窗口找到我们的接收者,并发送消息:

<p><pre>    <code class="prism language-python"><span class="token keyword">class</span> <span class="token class-name">sendMsg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>receiver<span class="token punctuation">,</span>msg<span class="token punctuation">)</span><span class="token punctuation">:</span>
        self<span class="token punctuation">.</span>receiver<span class="token operator">=</span>receiver
        self<span class="token punctuation">.</span>msg<span class="token operator">=</span>msg
        self<span class="token punctuation">.</span>setText<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">#设置剪贴版内容</span>
    <span class="token keyword">def</span> <span class="token function">setText</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        w<span class="token punctuation">.</span>OpenClipboard<span class="token punctuation">(</span><span class="token punctuation">)</span>
        w<span class="token punctuation">.</span>EmptyClipboard<span class="token punctuation">(</span><span class="token punctuation">)</span>
        w<span class="token punctuation">.</span>SetClipboardData<span class="token punctuation">(</span>win32con<span class="token punctuation">.</span>CF_UNICODETEXT<span class="token punctuation">,</span> self<span class="token punctuation">.</span>msg<span class="token punctuation">)</span>
        w<span class="token punctuation">.</span>CloseClipboard<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">#找到消息发送窗口,发送消息</span>
    <span class="token keyword">def</span> <span class="token function">sendmsg</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        qq<span class="token operator">=</span>win32gui<span class="token punctuation">.</span>FindWindow<span class="token punctuation">(</span><span class="token boolean">None</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>receiver<span class="token punctuation">)</span>
        win32gui<span class="token punctuation">.</span>SendMessage<span class="token punctuation">(</span>qq<span class="token punctuation">,</span>win32con<span class="token punctuation">.</span>WM_PASTE <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
        win32gui<span class="token punctuation">.</span>SendMessage<span class="token punctuation">(</span>qq<span class="token punctuation">,</span> win32con<span class="token punctuation">.</span>WM_KEYDOWN<span class="token punctuation">,</span> win32con<span class="token punctuation">.</span>VK_RETURN<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"sucessfuly send"</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>msg<span class="token punctuation">)</span>
</code></pre></p>
获取消息

我们发送的消息来自哪里?这里我复制了之前在群里的留言记录,然后过滤掉了一些没用的信息,存到了一个文件里。然后从文件中随机获取一条消息,提供给消息发送类

(1)复制群消息记录并粘贴到txt中

p>

(2)过滤掉标题、换行等,写入message.txt文件:

<p><pre>    <code class="prism language-python"><span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        f<span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">&#39;test.txt&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;r&#39;</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">&#39;utf-8&#39;</span><span class="token punctuation">)</span>
        lines<span class="token operator">=</span><span class="token string">""</span>
        <span class="token keyword">for</span> x <span class="token keyword">in</span> f<span class="token punctuation">:</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">&#39;【&#39;</span> <span class="token keyword">in</span> x<span class="token punctuation">)</span> <span class="token operator">or</span> <span class="token punctuation">(</span>x<span class="token operator">==</span><span class="token string">&#39;\n&#39;</span><span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token keyword">continue</span>
                lines<span class="token operator">+=</span>x


        f<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
        f<span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">&#39;message.txt&#39;</span><span class="token punctuation">,</span><span class="token string">&#39;w&#39;</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">&#39;utf-8&#39;</span><span class="token punctuation">)</span>
        f<span class="token punctuation">.</span>write<span class="token punctuation">(</span>lines<span class="token punctuation">)</span>
        f<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></p>
您可以看到过滤后的消息要简单得多

(3)从消息文件中获取随机消息:

<p><pre>    <code class="prism language-python"><span class="token keyword">def</span> <span class="token function">getmessage</span><span class="token punctuation">(</span>fileName<span class="token punctuation">)</span><span class="token punctuation">:</span>
    f<span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span>fileName<span class="token punctuation">,</span><span class="token string">&#39;r&#39;</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">&#39;utf-8&#39;</span><span class="token punctuation">)</span>
    lines<span class="token operator">=</span>f<span class="token punctuation">.</span>readlines<span class="token punctuation">(</span><span class="token punctuation">)</span>
    f<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> choice<span class="token punctuation">(</span>lines<span class="token punctuation">)</span>
</code></pre></p>
时间

对于计时,我们可以使用时间库中的sleep,单位为s,用while循环包裹:

<p><pre>    <code class="prism language-python"><span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    receiver<span class="token operator">=</span><span class="token string">&#39;XXXX&#39;</span><span class="token comment">#这里填入接收者的备注名</span>
    <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
        msg<span class="token operator">=</span>getmessage<span class="token punctuation">(</span><span class="token string">&#39;message.txt&#39;</span><span class="token punctuation">)</span>
        qq<span class="token operator">=</span>sendMsg<span class="token punctuation">(</span>receiver<span class="token punctuation">,</span>msg<span class="token punctuation">)</span>
        qq<span class="token punctuation">.</span>sendmsg<span class="token punctuation">(</span><span class="token punctuation">)</span>
        time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span>randrange<span class="token punctuation">(</span><span class="token number">60</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token operator">*</span><span class="token number">60</span><span class="token punctuation">,</span><span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></p>
运行代码

最终代码实现如下:

<p><pre>    <code class="prism language-python"><span class="token keyword">import</span> win32con
<span class="token keyword">import</span> win32gui
<span class="token keyword">import</span> win32clipboard <span class="token keyword">as</span> w
<span class="token keyword">import</span> time
<span class="token keyword">from</span> random <span class="token keyword">import</span> choice
<span class="token keyword">from</span> random <span class="token keyword">import</span> randrange
<span class="token keyword">class</span> <span class="token class-name">sendMsg</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>receiver<span class="token punctuation">,</span>msg<span class="token punctuation">)</span><span class="token punctuation">:</span>


        self<span class="token punctuation">.</span>receiver<span class="token operator">=</span>receiver
        self<span class="token punctuation">.</span>msg<span class="token operator">=</span>msg
        self<span class="token punctuation">.</span>setText<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">#设置剪贴版内容</span>
    <span class="token keyword">def</span> <span class="token function">setText</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        w<span class="token punctuation">.</span>OpenClipboard<span class="token punctuation">(</span><span class="token punctuation">)</span>
        w<span class="token punctuation">.</span>EmptyClipboard<span class="token punctuation">(</span><span class="token punctuation">)</span>
        w<span class="token punctuation">.</span>SetClipboardData<span class="token punctuation">(</span>win32con<span class="token punctuation">.</span>CF_UNICODETEXT<span class="token punctuation">,</span> self<span class="token punctuation">.</span>msg<span class="token punctuation">)</span>
        w<span class="token punctuation">.</span>CloseClipboard<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment">#发送消息</span>
    <span class="token keyword">def</span> <span class="token function">sendmsg</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        qq<span class="token operator">=</span>win32gui<span class="token punctuation">.</span>FindWindow<span class="token punctuation">(</span><span class="token boolean">None</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>receiver<span class="token punctuation">)</span>
        win32gui<span class="token punctuation">.</span>SendMessage<span class="token punctuation">(</span>qq<span class="token punctuation">,</span>win32con<span class="token punctuation">.</span>WM_PASTE <span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
        win32gui<span class="token punctuation">.</span>SendMessage<span class="token punctuation">(</span>qq<span class="token punctuation">,</span> win32con<span class="token punctuation">.</span>WM_KEYDOWN<span class="token punctuation">,</span> win32con<span class="token punctuation">.</span>VK_RETURN<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"sucessfuly send"</span><span class="token punctuation">,</span>self<span class="token punctuation">.</span>msg<span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">getmessage</span><span class="token punctuation">(</span>fileName<span class="token punctuation">)</span><span class="token punctuation">:</span>
    f<span class="token operator">=</span><span class="token builtin">open</span><span class="token punctuation">(</span>fileName<span class="token punctuation">,</span><span class="token string">&#39;r&#39;</span><span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">&#39;utf-8&#39;</span><span class="token punctuation">)</span>
    lines<span class="token operator">=</span>f<span class="token punctuation">.</span>readlines<span class="token punctuation">(</span><span class="token punctuation">)</span>
    f<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> choice<span class="token punctuation">(</span>lines<span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    receiver<span class="token operator">=</span><span class="token string">&#39;xxxx&#39;</span><span class="token comment">#这里填入接收者的备注名</span>
    <span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
        msg<span class="token operator">=</span>getmessage<span class="token punctuation">(</span><span class="token string">&#39;message.txt&#39;</span><span class="token punctuation">)</span>
        qq<span class="token operator">=</span>sendMsg<span class="token punctuation">(</span>receiver<span class="token punctuation">,</span>msg<span class="token punctuation">)</span>
        qq<span class="token punctuation">.</span>sendmsg<span class="token punctuation">(</span><span class="token punctuation">)</span>
        time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span>randrange<span class="token punctuation">(</span><span class="token number">60</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token operator">*</span><span class="token number">60</span><span class="token punctuation">,</span><span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">&#39;__main__&#39;</span><span class="token punctuation">:</span>
    main<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></p>
p>

好吧,我们回家吧。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则