用Python制作一个自动回复机器人

Posted by Supertramp on September 22, 2019

用Python制作一个自动回复机器人

本教程使用Python中pyautogui库的图片识别定位功能和模拟鼠标键盘操作功能来达到自动回复的目的,所以理论上支持所有聊天软件,这里使用Skype网页版

软件需要实现的功能

实时检测聊天窗口中的新消息和用户ID,将消息内容传递给第三方对话机器人,使用第三方机器人返回的对话内容回复消息

检测新消息

仔细观察Skype在线版消息框出现的位置会发现最后一个消息框与输入框的距离总是固定的 image.png 截取一张包含消息框边缘和输入框的图片 使用pyautogui库中的locateCenterOnScreen来返回图片中心点所在位置

import pyautogui
position = pyautogui.locateCenterOnScreen("./py/new_msg.png",grayscale = True)

grayscale = True 表示识别图片灰阶模式,可以加快图片识别速度。

获取消息内容

识别到新消息后,使用MoveTo将鼠标移动到指定位置 再使用move将鼠标网上移动到消息框中文字所在位置 点击鼠标右键,移动到复制所在位置并点击鼠标 image.png 使用pyperclip中的paste来获取刚才复制的消息内容

import pyautogui
from pyperclip import copy, paste

pyautogui.MoveTo(position)
pyautogui.move(0,-55)
pyautogui.click()
pyautogui.click(button='right')
pyautogui.move(10,-90)
pyautogui.click()
content = paste()

move中的数值表示移动多少像素 也可以使用同样的方法来获取到用户的昵称

将消息传递到第三方对话机器人

这里使用的是小思智能问答机器人,注册成功后将获取到的appid 填入到代码中 最后将小思返回的对话粘贴到输入框中并点击发送

api = r'https://api.ownthink.com/bot'
data = {'spoken':content,
'appid':'',
'userid':id
}
jsondata = json.dumps(data)
r = requests.post(api,data = jsondata)
r_json = json.loads(r.content)
r_msg = r_json["data"]["info"]["text"]
position = pyautogui.locateCenterOnScreen("./py/message.png",grayscale = True)
pyautogui.moveTo(position)
print ("回复: "+r_msg)
pyautogui.click()
copy(r_msg+"\n(这是一条自动回复, @"+id+")")
hotkey('ctrl','v')
press ("enter")

pyautogui的typewrite只支持英文字符的输入 这里借助pyperclip中的copy来传递中文内容

最后加入while循环以使程序持续运行

全部代码:

import pyautogui
from time import sleep
from pyautogui import press, hotkey, typewrite
import requests
import json
from pyperclip import copy, paste
import pyperclip
import re


while True:
    running = True
    while running:
        try:
            position = pyautogui.locateCenterOnScreen("./py/skype.png",grayscale = True)
            pyautogui.moveTo(position)
            pyautogui.move(0,-55)
            pyautogui.click()
            pyautogui.click(button='right')
            pyautogui.move(10,-90)
            pyautogui.click()
            content = paste()
            pyautogui.moveTo(position)
            pyautogui.move(0,-90)
            pyautogui.click()
            pyautogui.click()
            pyautogui.click()
            hotkey('ctrl','c')
            id = paste()
            id = str(id)
            id = id.replace('\n','')
            id = id[:-7]
            print (id+": "+content)
            api = r'https://api.ownthink.com/bot'
            data = {'spoken':content,
            'appid':'***',
            'userid':id
            }
            jsondata = json.dumps(data)
            r = requests.post(api,data = jsondata)
            r_json = json.loads(r.content)
            r_msg = r_json["data"]["info"]["text"]
            position = pyautogui.locateCenterOnScreen("./py/message.png",grayscale = True)
            pyautogui.moveTo(position)
            print ("回复: "+r_msg)
            pyautogui.click()
            copy(r_msg+"\n(这是一条自动回复, @"+id+")")
            hotkey('ctrl','v')
            press ("enter")
            sleep (1)
            running = False
        except:
            try:
                position = pyautogui.locateCenterOnScreen("./py/bigg.png",grayscale = True)
                pyautogui.moveTo(position)
                pyautogui.move(-10,-55)
                pyautogui.click()
                pyautogui.click(button='right')
                pyautogui.move(10,-90)
                pyautogui.click()
                content = paste()
                print (content)
                pyautogui.moveTo(position)
                pyautogui.move(-10,-85)
                pyautogui.click()
                pyautogui.click()
                pyautogui.click()
                hotkey('ctrl','c')
                id = paste()
                id = str(id)
                id = id.replace('\n','')
                id = id[:-7]
                print (id+": "+content)
                api = r'https://api.ownthink.com/bot'
                data = {'spoken':content,
                'appid':'***',
                'userid':id
                }
                jsondata = json.dumps(data)
                r = requests.post(api,data = jsondata)
                r_json = json.loads(r.content)
                r_msg = r_json["data"]["info"]["text"]
                position = pyautogui.locateCenterOnScreen("./py/message.png",grayscale = True)
                pyautogui.moveTo(position)
                print ("回复: "+r_msg)
                pyautogui.click()
                copy(r_msg+"\n(这是一条自动回复, @"+id+")")
                hotkey('ctrl','v')
                press ("enter")
                sleep (1)
                running = False
            except:
                continue