Add weixin support and adapt to mojo-webqq 2.0

This commit is contained in:
Richard Chien
2016-12-29 23:45:34 +08:00
parent c2433aef13
commit 0af4a3dbdc
13 changed files with 240 additions and 54 deletions

View File

@ -11,9 +11,11 @@ __registry__ = cr = CommandRegistry()
def echo(args_text, ctx_msg):
msg_type = ctx_msg.get('type')
if msg_type == 'group_message':
api.send_group_message(gnumber=ctx_msg.get('gnumber'), content=args_text)
elif msg_type == 'message':
api.send_message(qq=ctx_msg.get('sender_qq'), content=args_text)
api.send_group_message(content=args_text, ctx_msg=ctx_msg)
elif msg_type == 'discuss_message':
api.send_discuss_message(content=args_text, ctx_msg=ctx_msg)
elif msg_type == 'friend_message':
api.send_friend_message(content=args_text, ctx_msg=ctx_msg)
@cr.register('chat', '聊天')
@ -23,8 +25,10 @@ def chat(args_text, ctx_msg):
'key': os.environ.get('TURING123_API_KEY'),
'info': args_text
}
if 'sender_qq' in ctx_msg:
data['userid'] = ctx_msg.get('sender_qq')
if ctx_msg.get('sender_uid'):
data['userid'] = ctx_msg.get('sender_uid')
elif ctx_msg.get('sender_id'):
data['userid'] = ctx_msg.get('sender_id')[-32:]
resp = requests.post(url, data=data)
if resp.status_code == 200:
json = resp.json()

View File

@ -1,4 +1,5 @@
import sqlite3
from functools import wraps
from datetime import datetime
import pytz
@ -25,6 +26,19 @@ def _open_db_conn():
return conn
def _check_target(func):
@wraps(func)
def wrapper(args_text, ctx_msg, *args, **kwargs):
target = get_target(ctx_msg)
if not target:
core.echo('似乎出错了,请稍后再试吧~', ctx_msg)
return
else:
return func(args_text, ctx_msg, *args, **kwargs)
return wrapper
_cmd_take = 'note.take'
_cmd_remove = 'note.remove'
@ -32,6 +46,7 @@ _cmd_remove = 'note.remove'
@cr.register('记笔记', '添加笔记')
@cr.register('take', 'add', hidden=True)
@cr.restrict(group_admin_only=True)
@_check_target
def take(args_text, ctx_msg, allow_interactive=True):
source = get_source(ctx_msg)
if allow_interactive and (not args_text or has_session(source, _cmd_take)):
@ -52,6 +67,7 @@ def take(args_text, ctx_msg, allow_interactive=True):
@cr.register('列出所有笔记')
@cr.register('list', hidden=True)
@_check_target
def list_all(_, ctx_msg):
conn = _open_db_conn()
target = get_target(ctx_msg)
@ -74,6 +90,7 @@ def list_all(_, ctx_msg):
@cr.register('删除笔记')
@cr.register('remove', 'delete', hidden=True)
@cr.restrict(group_admin_only=True)
@_check_target
def remove(args_text, ctx_msg, allow_interactive=True):
source = get_source(ctx_msg)
if allow_interactive and (not args_text or has_session(source, _cmd_remove)):
@ -101,6 +118,7 @@ def remove(args_text, ctx_msg, allow_interactive=True):
@cr.register('清空笔记', '清空所有笔记', '删除所有笔记')
@cr.register('clear', hidden=True)
@cr.restrict(group_admin_only=True)
@_check_target
def clear(_, ctx_msg):
conn = _open_db_conn()
target = get_target(ctx_msg)

View File

@ -1,6 +1,6 @@
import os
import re
from functools import reduce
from functools import reduce, wraps
import pytz
from apscheduler.schedulers.background import BackgroundScheduler
@ -60,8 +60,22 @@ def _call_commands(job_id, command_list, ctx_msg):
)
def _check_target(func):
@wraps(func)
def wrapper(args_text, ctx_msg, internal=False, *args, **kwargs):
target = get_target(ctx_msg)
if not target:
_send_fail_to_get_target_msg(ctx_msg, internal)
return None
else:
return func(args_text, ctx_msg, internal, *args, **kwargs)
return wrapper
@cr.register('add_job', 'add-job', 'add')
@cr.restrict(full_command_only=True, group_admin_only=True)
@_check_target
def add_job(args_text, ctx_msg, internal=False):
if args_text.strip() in ('', 'help', '-h', '--help') and not internal:
_send_add_job_help_msg(ctx_msg, internal)
@ -150,6 +164,7 @@ def add_job(args_text, ctx_msg, internal=False):
@cr.register('remove_job', 'remove-job', 'remove')
@cr.restrict(full_command_only=True, group_admin_only=True)
@_check_target
def remove_job(args_text, ctx_msg, internal=False):
job_id_without_suffix = args_text.strip()
if not job_id_without_suffix:
@ -167,6 +182,7 @@ def remove_job(args_text, ctx_msg, internal=False):
@cr.register('get_job', 'get-job', 'get')
@cr.restrict(full_command_only=True)
@_check_target
def get_job(args_text, ctx_msg, internal=False):
job_id_without_suffix = args_text.strip()
if not job_id_without_suffix:
@ -190,6 +206,7 @@ def get_job(args_text, ctx_msg, internal=False):
@cr.register('list_jobs', 'list-jobs', 'list')
@cr.restrict(full_command_only=True)
@_check_target
def list_jobs(_, ctx_msg, internal=False):
target = get_target(ctx_msg)
job_id_suffix = '_' + target
@ -219,6 +236,15 @@ def _send_text(text, ctx_msg, internal):
core.echo(text, ctx_msg)
def _send_fail_to_get_target_msg(ctx_msg, internal):
_send_text(
'无法获取 target可能因为不支持当前消息类型不支持微信群组消息'
'或由于延迟还没能加载到用户的固定 ID微信号',
ctx_msg,
internal
)
def _send_add_job_help_msg(ctx_msg, internal):
_send_text(
'此为高级命令!如果你不知道自己在做什么,请不要使用此命令。\n\n'

10
commands/sudo.py Normal file
View File

@ -0,0 +1,10 @@
from command import CommandRegistry
from commands import core
__registry__ = cr = CommandRegistry()
@cr.register('test')
@cr.restrict(full_command_only=True, superuser_only=True)
def test(_, ctx_msg):
core.echo('Your are the superuser!', ctx_msg)