-
Notifications
You must be signed in to change notification settings - Fork 5
/
robot.py
187 lines (174 loc) · 6.51 KB
/
robot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from werobot import WeRoBot
import config as cfg
import requests
import datetime
# 定义主要参数
myrobot = WeRoBot(token=cfg.token)
myrobot.config["APP_ID"] = cfg.appid
myrobot.config['ENCODING_AES_KEY'] = cfg.aeskey
urlmd = "http://127.0.0.1:6806/api/filetree/createDocWithMd"
openid = "你的微信openid" # 可以修改成多个openid,达到多人共同写入笔记本的目的
notebook = "你的笔记本ID"
apitoken = "你的思源笔记apitoken"
weekday_dict = {
'0': '日',
'1': '一',
'2': '二',
'3': '三',
'4': '四',
'5': '五',
'6': '六'
}
# 定义请求头
headers = {
'Content-Type': 'application/json',
'Authorization': f'Token {apitoken}'
}
headers2 = {
'Authorization': f'Token {apitoken}'
}
# 收到图片消息时的动作
@myrobot.image
def image_note(message, session):
# 判断用户openid是否有权限写入笔记本
if message.source == openid:
now = datetime.datetime.now()
year = now.strftime("%Y")
month = now.strftime("%m")
day = now.strftime("%d")
hour = now.strftime("%H")
minute = now.strftime("%M")
second = now.strftime("%S")
name = f"{year}-{month}-{day}--{hour}:{minute}:{second}"
path = f"{year}年{month}月{day}日星期{weekday_dict[now.strftime('%w')]}-{hour}:{minute}:{second}"
url = "http://127.0.0.1:6806/api/asset/upload" # 思源笔记上传文件的api
file_url = message.img
response = requests.get(file_url)
file_content = response.content
payload = {
"assetsDirPath": "/assets/",
"file[]": (f"{name}.jpg", file_content)
}
response = requests.post(url, files=payload, headers=headers2)
data = response.json()
pathp = data['data']['succMap'][f"{name}.jpg"]
data = {
"notebook": notebook,
"path": f"【图片】-{path}",
"markdown": f"![微信图片{name}]({pathp})" # 以markdown形式写入到思源笔记
}
response = requests.post(urlmd, headers=headers, json=data)
result = response.json()
if 'code' in result and result['code'] == 0:
return f"记录成功:{path}"
else:
return "记录失败"
else:
return f"此用户无权限:{message.source}"
# 定义收到文本消息的动作
@myrobot.text
def test_note(message, session):
# 判断用户openid是否有权限写入笔记本
if message.source == openid:
now = datetime.datetime.now()
year = now.strftime("%Y")
month = now.strftime("%m")
day = now.strftime("%d")
hour = now.strftime("%H")
minute = now.strftime("%M")
second = now.strftime("%S")
path = f"{year}年{month}月{day}日星期{weekday_dict[now.strftime('%w')]}-{hour}:{minute}:{second}"
data = {
"notebook": notebook,
"path": f"【文字】-{path}",
"markdown": message.content # 以markdown形式写入到思源笔记
}
response = requests.post(urlmd, headers=headers, json=data)
result = response.json()
if 'code' in result and result['code'] == 0:
return f"记录成功:{path}"
else:
return "记录失败"
else:
return f"此用户无权限:{message.source}"
# 定义收到语音消息的动作
@myrobot.voice
def voice_note(message, session):
# 判断用户openid是否有权限写入笔记本
if message.source == openid:
now = datetime.datetime.now()
year = now.strftime("%Y")
month = now.strftime("%m")
day = now.strftime("%d")
hour = now.strftime("%H")
minute = now.strftime("%M")
second = now.strftime("%S")
path = f"{year}年{month}月{day}日星期{weekday_dict[now.strftime('%w')]}-{hour}:{minute}:{second}"
data = {
"notebook": notebook,
"path": f"【语音】-{path}",
"markdown": message.recognition # 以markdown形式写入到思源笔记
}
response = requests.post(urlmd, headers=headers, json=data)
result = response.json()
if 'code' in result and result['code'] == 0:
return f"记录成功:{path}"
else:
return "记录失败"
else:
return f"此用户无权限:{message.source}"
# 定义收到位置消息的动作
@myrobot.location
def location_note(message, session):
if message.source == openid:
now = datetime.datetime.now()
year = now.strftime("%Y")
month = now.strftime("%m")
day = now.strftime("%d")
hour = now.strftime("%H")
minute = now.strftime("%M")
second = now.strftime("%S")
path = f"{year}年{month}月{day}日星期{weekday_dict[now.strftime('%w')]}-{hour}:{minute}:{second}"
location = message.location
scale = message.scale
latitude = str(location[0])
longitude = str(location[1])
scale_str = str(scale)
data = {
"notebook": notebook,
"path": f"【位置】-{path}",
"markdown": f"经:{longitude}\n纬:{latitude}\n放缩倍数:{scale_str}\n描述:{message.label}" # 以markdown形式写入到思源笔记
}
response = requests.post(urlmd, headers=headers, json=data)
result = response.json()
if 'code' in result and result['code'] == 0:
return f"记录成功:{path}"
else:
return "记录失败"
else:
return f"此用户无权限:{message.source}"
# 定义收到链接消息的动作
@myrobot.link
def link_note(message, session):
if message.source == openid:
now = datetime.datetime.now()
year = now.strftime("%Y")
month = now.strftime("%m")
day = now.strftime("%d")
hour = now.strftime("%H")
minute = now.strftime("%M")
second = now.strftime("%S")
path = f"{year}年{month}月{day}日星期{weekday_dict[now.strftime('%w')]}-{hour}:{minute}:{second}"
data = {
"notebook": notebook,
"path": f"【链接】-{path}",
"markdown": f"链接:{message.url}\n标题:{message.title}\n描述:{message.description}" # 以markdown形式写入到思源笔记
}
response = requests.post(urlmd, headers=headers, json=data)
result = response.json()
if 'code' in result and result['code'] == 0:
return f"记录成功:{path}"
else:
return "记录失败"
else:
return f"此用户无权限:{message.source}"