Skip to content

rename the hostie to service #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions examples/advanced/friendship-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
6 changes: 3 additions & 3 deletions examples/advanced/scheduler-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions examples/ding-dong-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
137 changes: 137 additions & 0 deletions examples/professional/feishu/echo_bot.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 0 additions & 1 deletion examples/professional/tencentaiplat/tencentai_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
wechaty~=0.6
wechaty-puppet-service~=0.5.0dev
2 changes: 1 addition & 1 deletion tests/smoke_testing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'