diff --git a/README.md b/README.md index 436b663..911c6b1 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,8 @@ Wechaty is a RPA SDK for Wechat **Individual** Account that can help you create ```sh # examples/ding-dong-bot.py : func-> main() # it must be donut token - export WECHATY_PUPPET=wechaty-puppet-hostie - export WECHATY_PUPPET_HOSTIE_TOKEN=your_token_at_here + export WECHATY_PUPPET=wechaty-puppet-service + export WECHATY_PUPPET_SERVICE_TOKEN=your_token_at_here ``` 4. Run the bot diff --git a/examples/advanced/friendship-bot.py b/examples/advanced/friendship-bot.py index f53d94b..81e4b8a 100644 --- a/examples/advanced/friendship-bot.py +++ b/examples/advanced/friendship-bot.py @@ -71,11 +71,11 @@ async def on_login(self, contact: Contact): async def main(): """Async Main Entry""" - if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ: + if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ: print(''' - Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables + Error: WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables You need a TOKEN to run the Java Wechaty. Please goto our README for details - https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token + https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_service_token ''') global bot diff --git a/examples/advanced/room-inviter-bot.py b/examples/advanced/room-inviter-bot.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/advanced/scheduler-bot.py b/examples/advanced/scheduler-bot.py index 3b3669f..b26faa6 100644 --- a/examples/advanced/scheduler-bot.py +++ b/examples/advanced/scheduler-bot.py @@ -64,11 +64,11 @@ async def tick(bot: Wechaty): async def main(): """Async Main Entry""" - if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ: + if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ: print(''' - Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables + Error: WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables You need a TOKEN to run the Java Wechaty. Please goto our README for details - https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token + https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_service_token ''') global bot diff --git a/examples/ding-dong-bot.py b/examples/ding-dong-bot.py index de4e5aa..62523c1 100755 --- a/examples/ding-dong-bot.py +++ b/examples/ding-dong-bot.py @@ -59,13 +59,13 @@ async def main(): Async Main Entry """ # - # Make sure we have set WECHATY_PUPPET_HOSTIE_TOKEN in the environment variables. + # Make sure we have set WECHATY_PUPPET_SERVICE_TOKEN in the environment variables. # - if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ: + if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ: print(''' - Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables + Error: WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables You need a TOKEN to run the Java Wechaty. Please goto our README for details - https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token + https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_service_token ''') bot = Wechaty() diff --git a/examples/professional/feishu/echo_bot.py b/examples/professional/feishu/echo_bot.py new file mode 100644 index 0000000..695c3e5 --- /dev/null +++ b/examples/professional/feishu/echo_bot.py @@ -0,0 +1,137 @@ +#!/usr/bin/env python +# --coding:utf-8-- + +from http.server import BaseHTTPRequestHandler, HTTPServer +from os import path +import os +import json +from urllib import request, parse + + +APP_ID = os.getenv('FEISHU_APP_ID') +APP_SECRET = os.getenv("FEISHU_APP_SECRET") +APP_VERIFICATION_TOKEN = os.getenv("FEISHU_APP_VERIFICATION_TOKEN") + +class RequestHandler(BaseHTTPRequestHandler): + def do_POST(self): + # 解析请求 body + req_body = self.rfile.read(int(self.headers['content-length'])) + obj = json.loads(req_body.decode("utf-8")) + print(req_body) + + # 校验 verification token 是否匹配,token 不匹配说明该回调并非来自开发平台 + token = obj.get("token", "") + if token != APP_VERIFICATION_TOKEN: + print("verification token not match, token =", token) + self.response("") + return + + # 根据 type 处理不同类型事件 + type = obj.get("type", "") + if "url_verification" == type: # 验证请求 URL 是否有效 + self.handle_request_url_verify(obj) + elif "event_callback" == type: # 事件回调 + # 获取事件内容和类型,并进行相应处理,此处只关注给机器人推送的消息事件 + event = obj.get("event") + if event.get("type", "") == "message": + self.handle_message(event) + return + return + + def handle_request_url_verify(self, post_obj): + # 原样返回 challenge 字段内容 + challenge = post_obj.get("challenge", "") + rsp = {'challenge': challenge} + self.response(json.dumps(rsp)) + return + + def handle_message(self, event): + # 此处只处理 text 类型消息,其他类型消息忽略 + msg_type = event.get("msg_type", "") + if msg_type != "text": + print("unknown msg_type =", msg_type) + self.response("") + return + + # 调用发消息 API 之前,先要获取 API 调用凭证:tenant_access_token + access_token = self.get_tenant_access_token() + if access_token == "": + self.response("") + return + + # 机器人 echo 收到的消息 + if event.get('text') == 'ding': + self.send_message(access_token, event.get("open_id"), 'dong') + self.response("") + return + + def response(self, body): + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + self.wfile.write(body.encode()) + + def get_tenant_access_token(self): + url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/" + headers = { + "Content-Type" : "application/json" + } + req_body = { + "app_id": APP_ID, + "app_secret": APP_SECRET + } + + data = bytes(json.dumps(req_body), encoding='utf8') + req = request.Request(url=url, data=data, headers=headers, method='POST') + try: + response = request.urlopen(req) + except Exception as e: + print(e.read().decode()) + return "" + + rsp_body = response.read().decode('utf-8') + rsp_dict = json.loads(rsp_body) + code = rsp_dict.get("code", -1) + if code != 0: + print("get tenant_access_token error, code =", code) + return "" + return rsp_dict.get("tenant_access_token", "") + + def send_message(self, token, open_id, text): + url = "https://open.feishu.cn/open-apis/message/v4/send/" + + headers = { + "Content-Type": "application/json", + "Authorization": "Bearer " + token + } + req_body = { + "open_id": open_id, + "msg_type": "text", + "content": { + "text": text + } + } + + data = bytes(json.dumps(req_body), encoding='utf8') + req = request.Request(url=url, data=data, headers=headers, method='POST') + try: + response = request.urlopen(req) + except Exception as e: + print(e.read().decode()) + return + + rsp_body = response.read().decode('utf-8') + rsp_dict = json.loads(rsp_body) + code = rsp_dict.get("code", -1) + if code != 0: + print("send message error, code = ", code, ", msg =", rsp_dict.get("msg", "")) + +def run(): + port = 8000 + server_address = ('', port) + httpd = HTTPServer(server_address, RequestHandler) + print("start.....") + httpd.serve_forever() + +if __name__ == '__main__': + run() diff --git a/examples/professional/tencentaiplat/tencentai_bot.py b/examples/professional/tencentaiplat/tencentai_bot.py index ebaba96..3df38fd 100644 --- a/examples/professional/tencentaiplat/tencentai_bot.py +++ b/examples/professional/tencentaiplat/tencentai_bot.py @@ -3,7 +3,6 @@ from typing import Optional, Union from wechaty_puppet import PuppetOptions, FileBox # type: ignore -from wechaty_puppet_hostie import HostiePuppet # type: ignore from wechaty import Wechaty, Contact from wechaty.user import Message, Room diff --git a/requirements.txt b/requirements.txt index b6f64f1..06cb9ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ wechaty~=0.6 +wechaty-puppet-service~=0.5.0dev \ No newline at end of file diff --git a/tests/smoke_testing_test.py b/tests/smoke_testing_test.py index 6853794..1803452 100644 --- a/tests/smoke_testing_test.py +++ b/tests/smoke_testing_test.py @@ -13,6 +13,6 @@ def test_smoke_testing() -> None: """ wechaty """ - # os.environ['WECHATY_PUPPET_HOSTIE_TOKEN'] = 'test' + # os.environ['WECHATY_PUPPET_SERVICE_TOKEN'] = 'test' # bot = Wechaty() assert Wechaty, 'should be imported successfully'