安装werobot
pip install werobot
werobot github
https://github.com/whtsky/WeRoBot
werobot 文档
https://werobot.readthedocs.io/zh_CN/latest/index.html
示例代码 wechat.py
# -*- coding: utf-8 -*-
import werobot
robot = werobot.WeRoBot(token='tokenhere', enable_session=True)
@robot.subscribe
def subscribe(message):
return 'Hello My Friend!'
@robot.unsubscribe
def unsubscribe(message):
return 'Hello My Friend!'
@robot.text
def text(message, session):
last = session.get("last", None)
if last:
return last
session["last"] = message.content
return '这是你第一次和我说话'
@robot.filter("帮助")
def show_help(message):
return """
帮助
XXXXX
"""
@robot.click
def click(message):
if message.key == "abort":
return "I'm a robot"
robot.run(port=10001)
nginx 配置
server {
listen 80;
server_name wechat.minws.com;
access_log off;
location ~ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:10001;
}
}
启动文件 /etc/init.d/wechat
#!/bin/sh
### BEGIN INIT INFO
# Provides: wechat
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: wechat
# Description:
#
### END INIT INFO
NAME=wechat.py
DAEMON=/data/wwwroot/wechat.minws.com/$NAME
case "$1" in
start)
echo "Starting $NAME..."
nohup python $DAEMON >/dev/null 2>&1 &
;;
stop)
echo "Stopping $NAME..."
sudo ps -ef|grep $NAME|grep -v grep|cut -c 9-15|xargs kill -9
;;
restart)
$0 stop && sleep 2 && $0 start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0
自启动
chkconfig wechat on
评论 (0)