mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-27 08:11:38 +00:00
Initial commit
This commit is contained in:
38
commands/core.py
Normal file
38
commands/core.py
Normal file
@ -0,0 +1,38 @@
|
||||
import os
|
||||
import requests
|
||||
|
||||
from command import CommandRegistry
|
||||
from apiclient import client as api
|
||||
|
||||
__registry__ = cr = CommandRegistry()
|
||||
|
||||
|
||||
@cr.register('echo', '重复', '跟我念')
|
||||
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)
|
||||
|
||||
|
||||
@cr.register('chat', '聊天')
|
||||
def chat(args_text, ctx_msg):
|
||||
url = 'http://www.tuling123.com/openapi/api'
|
||||
data = {
|
||||
'key': os.environ.get('TURING123_API_KEY'),
|
||||
'info': args_text
|
||||
}
|
||||
if 'sender_qq' in ctx_msg:
|
||||
data['userid'] = ctx_msg.get('sender_qq')
|
||||
resp = requests.post(url, data=data)
|
||||
if resp.status_code == 200:
|
||||
json = resp.json()
|
||||
if int(json.get('code', 0)) == 100000:
|
||||
reply = json.get('text', '')
|
||||
else:
|
||||
# Is not text type
|
||||
reply = '腊鸡图灵机器人返回了一堆奇怪的东西,就不发出来了'
|
||||
else:
|
||||
reply = '腊鸡图灵机器人出问题了,先不管他,过会儿再玩他'
|
||||
echo(reply, ctx_msg)
|
159
commands/note.py
Normal file
159
commands/note.py
Normal file
@ -0,0 +1,159 @@
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
|
||||
import pytz
|
||||
|
||||
from command import CommandRegistry
|
||||
from commands import core
|
||||
from interactive import get_session, has_session, remove_session
|
||||
from little_shit import get_default_db_path, get_source, get_target
|
||||
|
||||
__registry__ = cr = CommandRegistry()
|
||||
|
||||
_create_table_sql = """CREATE TABLE IF NOT EXISTS cmd_note (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
dt INTEGER NOT NULL,
|
||||
target TEXT NOT NULL
|
||||
)"""
|
||||
|
||||
|
||||
def _open_db_conn():
|
||||
conn = sqlite3.connect(get_default_db_path())
|
||||
conn.execute(_create_table_sql)
|
||||
conn.commit()
|
||||
return conn
|
||||
|
||||
|
||||
_cmd_take = 'note.take'
|
||||
_cmd_remove = 'note.remove'
|
||||
|
||||
|
||||
@cr.register('记笔记', '添加笔记')
|
||||
@cr.register('take', 'add', hidden=True)
|
||||
@cr.restrict(group_admin_only=True)
|
||||
def take(args_text, ctx_msg, force=False):
|
||||
source = get_source(ctx_msg)
|
||||
if not force and (not args_text or has_session(source, _cmd_take)):
|
||||
# Be interactive
|
||||
return _take_interactively(args_text, ctx_msg, source)
|
||||
|
||||
conn = _open_db_conn()
|
||||
dt_unix = int(datetime.now(tz=pytz.utc).timestamp())
|
||||
target = get_target(ctx_msg)
|
||||
conn.execute(
|
||||
'INSERT INTO cmd_note (content, dt, target) VALUES (?, ?, ?)',
|
||||
(args_text, dt_unix, target)
|
||||
)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
core.echo('好的,记下了~', ctx_msg)
|
||||
|
||||
|
||||
@cr.register('列出所有笔记')
|
||||
@cr.register('list', hidden=True)
|
||||
def list_all(_, ctx_msg):
|
||||
conn = _open_db_conn()
|
||||
target = get_target(ctx_msg)
|
||||
cursor = conn.execute('SELECT id, dt, content FROM cmd_note WHERE target = ?', (target,))
|
||||
rows = list(cursor)
|
||||
conn.close()
|
||||
if len(rows) == 0:
|
||||
core.echo('还没有笔记哦~', ctx_msg)
|
||||
return
|
||||
for row in rows:
|
||||
tz_china = pytz.timezone('Asia/Shanghai')
|
||||
dt_raw = datetime.fromtimestamp(row[1], tz=pytz.utc)
|
||||
core.echo('ID:' + str(row[0])
|
||||
+ '\n时间:' + dt_raw.astimezone(tz_china).strftime('%Y.%m.%d %H:%M')
|
||||
+ '\n内容:' + str(row[2]),
|
||||
ctx_msg)
|
||||
core.echo('以上~', ctx_msg)
|
||||
|
||||
|
||||
@cr.register('删除笔记')
|
||||
@cr.register('remove', 'delete', hidden=True)
|
||||
@cr.restrict(group_admin_only=True)
|
||||
def remove(args_text, ctx_msg, force=False):
|
||||
source = get_source(ctx_msg)
|
||||
if not force and (not args_text or has_session(source, _cmd_remove)):
|
||||
# Be interactive
|
||||
return _remove_interactively(args_text, ctx_msg, source)
|
||||
|
||||
try:
|
||||
note_id = int(args_text)
|
||||
except ValueError:
|
||||
# Failed to cast
|
||||
core.echo('你输入的 ID 格式不正确哦~应该是个数字才对~', ctx_msg)
|
||||
return
|
||||
conn = _open_db_conn()
|
||||
target = get_target(ctx_msg)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('DELETE FROM cmd_note WHERE target = ? AND id = ?', (target, note_id))
|
||||
if cursor.rowcount > 0:
|
||||
core.echo('删除成功了~', ctx_msg)
|
||||
else:
|
||||
core.echo('没找到这个 ID 的笔记哦~', ctx_msg)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
@cr.register('清空笔记', '清空所有笔记', '删除所有笔记')
|
||||
@cr.register('clear', hidden=True)
|
||||
@cr.restrict(group_admin_only=True)
|
||||
def clear(_, ctx_msg):
|
||||
conn = _open_db_conn()
|
||||
target = get_target(ctx_msg)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('DELETE FROM cmd_note WHERE target = ?', (target,))
|
||||
if cursor.rowcount > 0:
|
||||
core.echo('成功删除了所有的笔记,共 %s 条~' % cursor.rowcount, ctx_msg)
|
||||
else:
|
||||
core.echo('本来就没有笔记哦~', ctx_msg)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
_state_machines = {}
|
||||
|
||||
|
||||
def _take_interactively(args_text, ctx_msg, source):
|
||||
def wait_for_content(s, a, c):
|
||||
core.echo('请发送你要记录的内容:', c)
|
||||
s.state += 1
|
||||
|
||||
def save_content(s, a, c):
|
||||
take(a, c, force=True)
|
||||
return True
|
||||
|
||||
if _cmd_take not in _state_machines:
|
||||
_state_machines[_cmd_take] = (
|
||||
wait_for_content, # 0
|
||||
save_content # 1
|
||||
)
|
||||
|
||||
sess = get_session(source, _cmd_take)
|
||||
if _state_machines[_cmd_take][sess.state](sess, args_text, ctx_msg):
|
||||
# Done
|
||||
remove_session(source, _cmd_take)
|
||||
|
||||
|
||||
def _remove_interactively(args_text, ctx_msg, source):
|
||||
def wait_for_note_id(s, a, c):
|
||||
core.echo('请发送你要删除的笔记的 ID:', c)
|
||||
s.state += 1
|
||||
|
||||
def remove_note(s, a, c):
|
||||
remove(a, c, force=True)
|
||||
return True
|
||||
|
||||
if _cmd_remove not in _state_machines:
|
||||
_state_machines[_cmd_remove] = (
|
||||
wait_for_note_id, # 0
|
||||
remove_note # 1
|
||||
)
|
||||
|
||||
sess = get_session(source, _cmd_remove)
|
||||
if _state_machines[_cmd_remove][sess.state](sess, args_text, ctx_msg):
|
||||
# Done
|
||||
remove_session(source, _cmd_remove)
|
62
commands/zhihu.py
Normal file
62
commands/zhihu.py
Normal file
@ -0,0 +1,62 @@
|
||||
import re
|
||||
from datetime import date, timedelta
|
||||
|
||||
import requests
|
||||
|
||||
from little_shit import SkipException
|
||||
from command import CommandRegistry
|
||||
from commands import core
|
||||
|
||||
__registry__ = cr = CommandRegistry()
|
||||
|
||||
|
||||
@cr.register('zhihu', 'zhihu-daily', '知乎日报')
|
||||
def zhihu_daily(args_text, ctx_msg):
|
||||
param = args_text.strip()
|
||||
reply = None
|
||||
try:
|
||||
if not param:
|
||||
sub_url = '/latest'
|
||||
elif re.match('\d{8}', param) and param >= '20130519':
|
||||
thedate = date(year=int(param[:4]), month=int(param[4:6]), day=int(param[6:]))
|
||||
sub_url = '/before/' + (thedate + timedelta(days=1)).strftime('%Y%m%d')
|
||||
else:
|
||||
reply = '命令格式错误,正确的命令格式:\n' \
|
||||
'/zhihu\n' \
|
||||
'或\n' \
|
||||
'/zhihu 20161129\n' \
|
||||
'注意如果指定日期,格式一定要对,且日期需在 20130519 之后。'
|
||||
raise SkipException
|
||||
full_url = 'https://news-at.zhihu.com/api/4/news' + sub_url
|
||||
resp = requests.get(
|
||||
full_url,
|
||||
headers={
|
||||
'Host': 'news-at.zhihu.com',
|
||||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36'
|
||||
' (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
|
||||
}
|
||||
)
|
||||
if resp.status_code == 200:
|
||||
json = resp.json()
|
||||
if 'stories' not in json:
|
||||
reply = '获取知乎日报数据失败,知乎返回了一堆迷之数据'
|
||||
raise SkipException
|
||||
reply = ('今天' if sub_url == '/latest' else '这天') + '的知乎日报内容如下:'
|
||||
core.echo(reply, ctx_msg)
|
||||
step = 6 # Send 8 items per time
|
||||
items = list(reversed(json.get('stories')))
|
||||
for start in range(0, len(items), step):
|
||||
reply = ''
|
||||
for item in items[start:min(start + step, len(items))]:
|
||||
reply += item.get('title') + '\n' + \
|
||||
'https://daily.zhihu.com/story/' + str(item.get('id')) + '\n\n'
|
||||
reply = reply.rstrip()
|
||||
core.echo(reply, ctx_msg)
|
||||
return
|
||||
else:
|
||||
reply = '获取知乎日报数据失败,可能知乎服务器又宕机了(('
|
||||
raise SkipException
|
||||
except SkipException:
|
||||
reply = reply if reply else '发生了未知错误……'
|
||||
pass
|
||||
core.echo(reply, ctx_msg)
|
Reference in New Issue
Block a user