Skip to content
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
README.md -text whitespace=cr-at-eol
45 changes: 27 additions & 18 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
name: Build and Publish Package
on:
release:
types: [published]
jobs:
build-and-publish:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Build Package
run: |
python setup.py sdist bdist_wheel
- name: Publish Package
uses: pypa/gh-action-pypi-publish@master
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
name: Build and Publish Package
on:
release:
types: [published]
jobs:
build-and-publish:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.14'
- name: Install build tools
run: python -m pip install build
- name: Test
run: |
python -m pip install --editable .
python -m unittest discover -s tests -v
- name: Build Package
run: python -m build
- name: Publish Package
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
52 changes: 52 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Test

on:
pull_request:
push:
branches:
- main

permissions:
contents: read

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
python:
- '3.8'
- '3.9'
- '3.10'
- '3.11'
- '3.12'
- '3.13'
- '3.14'
include:
- os: windows-latest
python: '3.10'
- os: windows-latest
python: '3.14'

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
cache: pip
cache-dependency-path: setup.py

- name: Install
run: python -m pip install --editable .

- name: Test
run: python -m unittest discover -s tests -v

- name: Compile package and examples
run: python -m compileall -q dingtalk_stream tests examples
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ class CalcBotHandler(dingtalk_stream.ChatbotHandler):
incoming_message = dingtalk_stream.ChatbotMessage.from_dict(callback.data)
expression = incoming_message.text.content.strip()
try:
result = eval(expression)
operands = [float(part.strip()) for part in expression.split('+')]
if len(operands) < 2:
raise ValueError('only addition expressions are supported')
result = sum(operands)
if result.is_integer():
result = int(result)
except Exception as e:
result = 'Error: %s' % e
self.logger.info('%s = %s' % (expression, result))
Expand Down Expand Up @@ -97,14 +102,29 @@ if __name__ == '__main__':

有的时候,你需要在已有的 ioloop 中使用钉钉 Stream 模式,不使用 `start_forever` 方法。

此时,可以使用 `client.start()` 代替 `client.start_forever()`。注意:需要在网络异常后重新启动
此时,可以使用 `client.start()` 代替 `client.start_forever()`。`start()` 会在网络异常后自动重连,
调用 `stop()` 可以中断重连等待并关闭当前连接:

```Python
client_task = asyncio.create_task(client.start())
try:
await client.start()
except (asyncio.exceptions.CancelledError,
websockets.exceptions.ConnectionClosedError) as e:
... # 处理网络断线异常
await run_application()
finally:
await client.stop()
await client_task
```

可以通过 `websocket_connect_options` 透传 `websockets.connect()` 参数,例如调整握手和心跳超时:

```Python
client = dingtalk_stream.DingTalkStreamClient(
credential,
websocket_connect_options={
'open_timeout': 10,
'ping_interval': 20,
'ping_timeout': 20,
},
)
```

## 开发教程
Expand Down
19 changes: 0 additions & 19 deletions dingtalk_stream/frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,6 @@ def __init__(self):
self.data = {}
self.extensions = {}

@classmethod
def from_dict(cls, d):
msg = SystemMessage()
data = ''
for name, value in d.items():
if name == 'specVersion':
msg.spec_version = value
elif name == 'data':
data = value
elif name == 'type':
pass
elif name == 'headers':
msg.headers = Headers.from_dict(value)
else:
msg.extensions[name] = value
if data:
msg.data = json.loads(data)
return msg

def __str__(self):
return 'SystemMessage(spec_version=%s, type=%s, headers=%s, data=%s, extensions=%s)' % (
self.spec_version,
Expand Down
Loading
Loading