1.材料准备
1. 一台服务器(因为我用的ubuntu)
2. 安装docker(我用宝塔面板一键安装)
3. 一个puppet token(一个手机号可嫖7天,点这里免费领)
2.极速上手
1.在服务器上创建一个sh文件,把下面内容复制进去
docker pull wechaty/wechaty:0.65
export WECHATY_PUPPET_PADLOCAL_TOKEN=puppet_padlocal_******
export WECHATY_PUPPET_SERVER_PORT=5701
export WECHATY_PUPPET=wechaty-puppet-padlocal
export WECHATY_LOG=verbose
docker run \
--rm \
-ti \
-e WECHATY_LOG \
-e WECHATY_PUPPET \
-e WECHATY_PUPPET_PADLOCAL_TOKEN \
-e WECHATY_PUPPET_SERVER_PORT \
-e WECHATY_TOKEN="$WECHATY_PUPPET_PADLOCAL_TOKEN" \
-p "$WECHATY_PUPPET_SERVER_PORT:$WECHATY_PUPPET_SERVER_PORT" \
wechaty/wechaty:0.65
然后把第二行的等号的后面部分换成你的刚刚得到的puppet token
2. 运行sh(我开了一个screen方便随时看进度,你们可以随意,我第一次运行花了几个小时才下好,不知道是不是我服务器的问题)
3. 等到有链接出来了打开就是登录微信的二维码了,扫码登录即可
3.python连接
注意:py最适配的版本应该是3.8,py3.11会报错
先安装包:
pip3 install wechaty
or
pip install wechaty
这里是代码示例:
# 为 urllib3 添加 get_host 函数
from urllib.parse import urlparse
def get_host(url: str) -> (str, str, str):
parsed_url = urlparse(url)
return parsed_url.scheme, parsed_url.netloc, parsed_url.path
import urllib3
urllib3.get_host = get_host
#有些朋友的urllib3可能会报错,说没有get_host函数,这段代码可以解决
#如果不需要的话删掉就行了
from wechaty import Wechaty, Message
import asyncio, os, time
# 填入的还是你前面申请的token
os.environ["WECHATY_PUPPET_SERVICE_TOKEN"] = "puppet_padlocal_d492875d5c0d4fb3b21c23ebf6cc7d7e"
# docker运行的主机地址+端口
os.environ["WECHATY_PUPPET_SERVICE_ENDPOINT"] = "172.17.0.2:5701"
bot = Wechaty()
class MyBot(Wechaty):
async def on_message(self, msg: Message):
from_contact = msg.talker()
talker: Contact = msg.talker()
text = msg.text()
room = msg.room()
if room is not None:
conversation: Union[
Room, Contact] = from_contact if room is None else room
await conversation.ready()
await conversation.say('hello')
asyncio.run(MyBot().start())