Skip to content

Commit b94d12e

Browse files
authored
Merge pull request #37 from wj-Mcat/master
rename the `hostie` to `service`
2 parents fc6b003 + 3c9877c commit b94d12e

File tree

8 files changed

+149
-12
lines changed

8 files changed

+149
-12
lines changed

examples/advanced/friendship-bot.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ async def on_login(self, contact: Contact):
7171

7272
async def main():
7373
"""Async Main Entry"""
74-
if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ:
74+
if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ:
7575
print('''
76-
Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables
76+
Error: WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables
7777
You need a TOKEN to run the Java Wechaty. Please goto our README for details
78-
https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token
78+
https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_service_token
7979
''')
8080

8181
global bot

examples/advanced/room-inviter-bot.py

Whitespace-only changes.

examples/advanced/scheduler-bot.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ async def tick(bot: Wechaty):
6464

6565
async def main():
6666
"""Async Main Entry"""
67-
if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ:
67+
if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ:
6868
print('''
69-
Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables
69+
Error: WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables
7070
You need a TOKEN to run the Java Wechaty. Please goto our README for details
71-
https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token
71+
https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_service_token
7272
''')
7373

7474
global bot

examples/ding-dong-bot.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ async def main():
5959
Async Main Entry
6060
"""
6161
#
62-
# Make sure we have set WECHATY_PUPPET_HOSTIE_TOKEN in the environment variables.
62+
# Make sure we have set WECHATY_PUPPET_SERVICE_TOKEN in the environment variables.
6363
#
64-
if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ:
64+
if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ:
6565
print('''
66-
Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables
66+
Error: WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables
6767
You need a TOKEN to run the Java Wechaty. Please goto our README for details
68-
https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token
68+
https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_service_token
6969
''')
7070

7171
bot = Wechaty()
+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/bin/env python
2+
# --coding:utf-8--
3+
4+
from http.server import BaseHTTPRequestHandler, HTTPServer
5+
from os import path
6+
import os
7+
import json
8+
from urllib import request, parse
9+
10+
11+
APP_ID = os.getenv('FEISHU_APP_ID')
12+
APP_SECRET = os.getenv("FEISHU_APP_SECRET")
13+
APP_VERIFICATION_TOKEN = os.getenv("FEISHU_APP_VERIFICATION_TOKEN")
14+
15+
class RequestHandler(BaseHTTPRequestHandler):
16+
def do_POST(self):
17+
# 解析请求 body
18+
req_body = self.rfile.read(int(self.headers['content-length']))
19+
obj = json.loads(req_body.decode("utf-8"))
20+
print(req_body)
21+
22+
# 校验 verification token 是否匹配,token 不匹配说明该回调并非来自开发平台
23+
token = obj.get("token", "")
24+
if token != APP_VERIFICATION_TOKEN:
25+
print("verification token not match, token =", token)
26+
self.response("")
27+
return
28+
29+
# 根据 type 处理不同类型事件
30+
type = obj.get("type", "")
31+
if "url_verification" == type: # 验证请求 URL 是否有效
32+
self.handle_request_url_verify(obj)
33+
elif "event_callback" == type: # 事件回调
34+
# 获取事件内容和类型,并进行相应处理,此处只关注给机器人推送的消息事件
35+
event = obj.get("event")
36+
if event.get("type", "") == "message":
37+
self.handle_message(event)
38+
return
39+
return
40+
41+
def handle_request_url_verify(self, post_obj):
42+
# 原样返回 challenge 字段内容
43+
challenge = post_obj.get("challenge", "")
44+
rsp = {'challenge': challenge}
45+
self.response(json.dumps(rsp))
46+
return
47+
48+
def handle_message(self, event):
49+
# 此处只处理 text 类型消息,其他类型消息忽略
50+
msg_type = event.get("msg_type", "")
51+
if msg_type != "text":
52+
print("unknown msg_type =", msg_type)
53+
self.response("")
54+
return
55+
56+
# 调用发消息 API 之前,先要获取 API 调用凭证:tenant_access_token
57+
access_token = self.get_tenant_access_token()
58+
if access_token == "":
59+
self.response("")
60+
return
61+
62+
# 机器人 echo 收到的消息
63+
if event.get('text') == 'ding':
64+
self.send_message(access_token, event.get("open_id"), 'dong')
65+
self.response("")
66+
return
67+
68+
def response(self, body):
69+
self.send_response(200)
70+
self.send_header('Content-Type', 'application/json')
71+
self.end_headers()
72+
self.wfile.write(body.encode())
73+
74+
def get_tenant_access_token(self):
75+
url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
76+
headers = {
77+
"Content-Type" : "application/json"
78+
}
79+
req_body = {
80+
"app_id": APP_ID,
81+
"app_secret": APP_SECRET
82+
}
83+
84+
data = bytes(json.dumps(req_body), encoding='utf8')
85+
req = request.Request(url=url, data=data, headers=headers, method='POST')
86+
try:
87+
response = request.urlopen(req)
88+
except Exception as e:
89+
print(e.read().decode())
90+
return ""
91+
92+
rsp_body = response.read().decode('utf-8')
93+
rsp_dict = json.loads(rsp_body)
94+
code = rsp_dict.get("code", -1)
95+
if code != 0:
96+
print("get tenant_access_token error, code =", code)
97+
return ""
98+
return rsp_dict.get("tenant_access_token", "")
99+
100+
def send_message(self, token, open_id, text):
101+
url = "https://open.feishu.cn/open-apis/message/v4/send/"
102+
103+
headers = {
104+
"Content-Type": "application/json",
105+
"Authorization": "Bearer " + token
106+
}
107+
req_body = {
108+
"open_id": open_id,
109+
"msg_type": "text",
110+
"content": {
111+
"text": text
112+
}
113+
}
114+
115+
data = bytes(json.dumps(req_body), encoding='utf8')
116+
req = request.Request(url=url, data=data, headers=headers, method='POST')
117+
try:
118+
response = request.urlopen(req)
119+
except Exception as e:
120+
print(e.read().decode())
121+
return
122+
123+
rsp_body = response.read().decode('utf-8')
124+
rsp_dict = json.loads(rsp_body)
125+
code = rsp_dict.get("code", -1)
126+
if code != 0:
127+
print("send message error, code = ", code, ", msg =", rsp_dict.get("msg", ""))
128+
129+
def run():
130+
port = 8000
131+
server_address = ('', port)
132+
httpd = HTTPServer(server_address, RequestHandler)
133+
print("start.....")
134+
httpd.serve_forever()
135+
136+
if __name__ == '__main__':
137+
run()

examples/professional/tencentaiplat/tencentai_bot.py

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Optional, Union
44

55
from wechaty_puppet import PuppetOptions, FileBox # type: ignore
6-
from wechaty_puppet_hostie import HostiePuppet # type: ignore
76

87
from wechaty import Wechaty, Contact
98
from wechaty.user import Message, Room

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
wechaty~=0.6
2+
wechaty-puppet-service~=0.5.0dev

tests/smoke_testing_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313

1414
def test_smoke_testing() -> None:
1515
""" wechaty """
16-
# os.environ['WECHATY_PUPPET_HOSTIE_TOKEN'] = 'test'
16+
# os.environ['WECHATY_PUPPET_SERVICE_TOKEN'] = 'test'
1717
# bot = Wechaty()
1818
assert Wechaty, 'should be imported successfully'

0 commit comments

Comments
 (0)