Skip to content

Commit

Permalink
增加输入口令自动邀请进群、远程踢人功能、进群欢迎消息等
Browse files Browse the repository at this point in the history
  • Loading branch information
Humsen committed Mar 23, 2017
1 parent 7e4876d commit b6f7ff0
Showing 1 changed file with 80 additions and 19 deletions.
99 changes: 80 additions & 19 deletions wechatrobot_me_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import os
import threading

import re
import requests
import time
from wxpy import *
Expand Down Expand Up @@ -63,7 +65,7 @@ def remote_admin_up(msg):
thread.join()
return '已启动'

# 回复来自其他好友、群聊和公众号的消息
# 回复来自其他好友的消息
@robot.register([Friend])
def reply_my_friend(msg):
if (msg.is_at and msg.member == remote_admin and '休眠' in msg.text):
Expand All @@ -75,14 +77,28 @@ def reply_my_friend(msg):
tuling.do_reply(msg)

# 如果是群聊,但没有被 @,则不回复
@robot.register([Group])
@robot.register([Group],TEXT)
def auto_reply(msg):
if not (isinstance(msg.sender, Group) and not msg.is_at):
if (msg.is_at and msg.member == remote_admin and '休眠' in msg.text):
if(msg.is_at):
if ('休眠' in msg.text and msg.member == remote_admin ):
thread = threading.Thread(target=remote_down)
thread.start()
thread.join()
return '已休眠'
elif(msg.chat.is_owner and '移出' in msg.text and msg.member == remote_admin ):
try:
name_to_kick = re.search(r'移出\s*@(.+?)(?:\u2005?\s*$)', msg.text).group(1)
except AttributeError:
remote_admin.send('无法解析命令')
return

member_to_kick = ensure_one(msg.chat.search(name_to_kick))
if member_to_kick == remote_admin:
remote_admin.send('在尝试移出自己吗?')
return
else:
member_to_kick.remove()
return '已移出 [{}]'.format(name_to_kick)
else:
tuling.do_reply(msg)

Expand All @@ -104,26 +120,66 @@ def ignore_mps(msg):
# 啥也不做
return

"""
#忽略一些群
@robot.register(boring_group)
def ignore(msg):
# 啥也不做
return
"""

#接收一些群
@robot.register([group_1,group_3])
def ignore(msg):
def recieve_some(msg):
#if ('休眠' in msg.text):
if (msg.is_at and msg.member == remote_admin and '休眠' in msg.text):
if (msg.is_at and '休眠' in msg.text and msg.member == remote_admin ):
thread = threading.Thread(target=remote_down)
thread.start()
thread.join()
return '已休眠'
elif (msg.is_at and msg.chat.is_owner and '移出' in msg.text and msg.member == remote_admin):
try:
name_to_kick = re.search(r'移出\s*@(.+?)(?:\u2005?\s*$)', msg.text).group(1)
except AttributeError:
remote_admin.send('无法解析命令')
return

member_to_kick = ensure_one(msg.chat.search(name_to_kick))
if member_to_kick == remote_admin:
remote_admin.send('在尝试移出自己吗?')
return
else:
member_to_kick.remove()
return '已移出 [{}]'.format(name_to_kick)
else:
tuling.do_reply(msg)

# 验证入群口令
def valid(msg):
return 'wechat' in msg.text.lower()

# 邀请入群
def invite(user):
if user in group_3:
user.send('你已加入 {}'.format(group_3.nick_name))
else:
if(len(group_3) < 5):
group_3.add_members(user, use_invitation=True)
else:
group_3.add_members(user, use_invitation=False)

# 响应好友消息
@robot.register([Friend], msg_types=TEXT)
def exist_friends(msg):
if valid(msg):
invite(msg.sender)
else:
tuling.do_reply(msg)

welcome_text = '''******\U0001F389\U0001F389\U0001F389\U0001F389******\n
欢迎 @{} 加入本群!\n\n*******************************'''
# 新人欢迎消息
@robot.register([Group], NOTE)
def welcome(msg):
try:
new_member_name = re.search(r'邀请"(.+?)"|"(.+?)"通过', msg.text).group(1)
except AttributeError:
return

return welcome_text.format(new_member_name)

#接收管理员命令
@robot.register(remote_admin)
def remote_admin_command(msg):
Expand All @@ -144,7 +200,12 @@ def auto_accept_friends(msg):
new_friend = robot.accept_friend(msg.card)
# 或 new_friend = msg.card.accept()
# 向新的好友发送消息
new_friend.send('哈哈,我自动接受了你的好友请求')
new_friend.send('哈哈,我自动接受了你的好友请求。')

if valid(msg):
invite(new_friend)
else:
return

groups = robot.groups(True)
#整点报时
Expand All @@ -155,16 +216,16 @@ def auto_accept_friends(msg):

if (hour == '07' and minutes == '30' and seconds == '00'):
nowTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
group_1.send('早上好!\n起床啦,为您整点报时:\n{}\n-----今日天气-----\n{}'.format(nowTime,talks_robot(info='重庆沙坪坝区天气')))
group_3.send('早上好!\n起床啦,为您整点报时:\n{}\n-----今日天气-----\n{}'.format(nowTime,talks_robot(info='南京江宁区天气')))
group_1.send('早上好!\n起床啦,为您整点报时:\n{}\n--------今日天气--------\n{}'.format(nowTime,talks_robot(info='重庆沙坪坝区天气')))
group_3.send('早上好!\n起床啦,为您整点报时:\n{}\n--------今日天气--------\n{}'.format(nowTime,talks_robot(info='南京江宁区天气')))
elif(hour == '13' and minutes == '30' and seconds == '00'):
nowTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
for group in groups:
group.send('下午好!\n该干活了,为您整点报时:\n{}\n-----轻松一下-----\n{}'.format(nowTime,talks_robot(info='讲个笑话')))
group.send('下午好!\n该干活了,为您整点报时:\n{}\n--------轻松一下--------\n{}'.format(nowTime,talks_robot(info='讲个笑话')))
elif (hour == '00' and minutes == '00' and seconds == '00'):
nowTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
for group in groups:
group.send('晚上好!\nIt\'s 宵夜time,为您整点报时:\n{}\n-----晚安全世界-----'.format(nowTime))
group.send('晚上好!\nIt\'s 宵夜time,为您整点报时:\n{}\n--------晚安全世界--------'.format(nowTime))

time.sleep(1)

Expand Down

0 comments on commit b6f7ff0

Please sign in to comment.