Skip to content

Commit

Permalink
feat: add some providers, rename notify to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jetsung committed Dec 31, 2023
1 parent fa30263 commit df71e34
Show file tree
Hide file tree
Showing 31 changed files with 272 additions and 74 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ipush

APP 推送通知。支持往 **钉钉群、飞书群、Lark 群、Bark、Chanify、PushDeer、PushPlus、Showdoc、息知** 推送消息。
向 APP、微信平台推送通知。
支持往 **Telegram、钉钉群、飞书群、Lark 群、Bark、Chanify、PushDeer、PushPlus、Showdoc、息知、Alertzy、Notify** 推送消息。

---

Expand All @@ -26,7 +27,7 @@ pip install -U ipush
from ipush import Dingtalk

notify = Dingtalk("token", "secret")
notify.send("ipush test")
notify.send("iPush test")
```

## 支持平台
Expand All @@ -47,6 +48,8 @@ notify.send("ipush test")
| 状态 | **国外**平台 | 官网 | 文档 | 备注 |
| :------- | :----------- | :-------------------------------------------------------------------------------- | :--- | :--------------------------------------------------------------------------------------------------------------------------------------------------- |
|**** | **Telegram** | [https://core.telegram.org/bots/](https://core.telegram.org/bots/api#sendmessage) | - | 创建[Bot](https://t.me/BotFather)后,将 Bot 添加至群组或频道,再添加[获取 ChatId 的机器人进群组](https://t.me/getmyid_bot)(可移除),即可获得`ChatId` |
|| **Alertzy** | [https://alertzy.app/](https://alertzy.app/) | - | |
|| **Notify** | [https://notify.dev/](https://notify.dev/docs/quickstart) | - | 安装手机 APP,复制设备 ID(`Settings -> Device ID`),输入到 [`Playground`](https://notify.dev/playground),获取二维码,再使用手机 APP 扫描 |

## 开发

Expand Down
18 changes: 18 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,21 @@ notify.send("ipush test", "chat_id")
notify.seturl("https://self-hosted")
notify.send("ipush test custom url")
```

- **Alertzy**

```python
from ipush import Alertzy

notify = Alertzy("token")
notify.send("ipush test", "title")
```

- **Notify**

```python
from ipush import Notify

notify = Notify("token", "user_id")
notify.send("ipush test", "title")
```
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "ipush"
version = "0.3.0"
description = "APP 推送通知。支持往 钉钉群、飞书群、Lark 群、Bark、Chanify、PushDeer、PushPlus、Showdoc、息知 推送消息"
version = "0.4.0"
description = "向 APP、微信平台推送通知。支持 Telegram、钉钉群、飞书群、Lark 群、Bark 等平台"
authors = [
{ name = "Jetsung Chan", email = "[email protected]" }
]
Expand All @@ -10,7 +10,7 @@ dependencies = [
"lxml>=4.9.4",
]
readme = "README.md"
keywords = ["push", "notify", "dingtalk", "feishu", "lark", "bark", "chanify", "pushdeer", "pushplus", "showdoc", "xizhi"]
keywords = ["push", "notify", "telegram", "dingtalk", "feishu", "lark", "bark", "chanify", "pushdeer", "pushplus", "showdoc", "xizhi"]
requires-python = ">= 3.8"
classifiers = [
'Development Status :: 4 - Beta',
Expand All @@ -37,6 +37,7 @@ classifiers = [
Homepage = "https://git.jetsung.com/idev/pypush"
Documentation = "https://pypush.skiy.net/"
Repository = "https://github.com/idevsig/pypush"
Download = "https://github.com/idevsig/pypush/releases"

[build-system]
requires = ["hatchling"]
Expand Down
22 changes: 12 additions & 10 deletions src/ipush/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from .notify.bark import Bark
from .notify.chanify import Chanify
from .notify.dingtalk import Dingtalk
from .notify.feishu import Feishu
from .notify.lark import Lark
from .notify.pushdeer import PushDeer
from .notify.pushplus import PushPlus
from .notify.showdoc import Showdoc
from .notify.telegram import Telegram
from .notify.xizhi import Xizhi
from .provider.alertzy import Alertzy
from .provider.bark import Bark
from .provider.chanify import Chanify
from .provider.dingtalk import Dingtalk
from .provider.feishu import Feishu
from .provider.lark import Lark
from .provider.notify import Notify
from .provider.pushdeer import PushDeer
from .provider.pushplus import PushPlus
from .provider.showdoc import Showdoc
from .provider.telegram import Telegram
from .provider.xizhi import Xizhi
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from abc import abstractmethod

"""
Notify 推送通知
Provider 提供者
"""


class Notify(ABC):
class Provider(ABC):
@abstractmethod
def _signature(self):
"""
Expand Down
39 changes: 39 additions & 0 deletions src/ipush/provider/alertzy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json

from ..utils.fetch import Fetch
from ._provider import Provider


class Alertzy(Provider):
"""
Alertzy通知
"""

def __init__(self, token=''):
self.token = token

def _signature(self):
pass

def _geturl(self):
"""
生成请求的 URL
"""
return 'https://alertzy.app/send'

def send(self, message, title=''):
"""
发送通知
:param message: 消息内容
"""
req_url = self._geturl()

req = Fetch()

data = {
'accountKey': self.token,
'title': '新消息' if title == '' else title,
'message': message,
}
req.post(req_url, data)
return req.response
6 changes: 3 additions & 3 deletions src/ipush/notify/bark.py → src/ipush/provider/bark.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from ..utils.fetch import Fetch
from .notify import Notify
from ._provider import Provider


class Bark(Notify):
class Bark(Provider):
"""
Bark通知
"""
Expand Down Expand Up @@ -39,7 +39,7 @@ def send(self, message, title=''):
req.update_headers(headers)

data = {
'title': '' if title == '' else title,
'title': '新消息' if title == '' else title,
'body': message,
'device_key': self.token,
}
Expand Down
4 changes: 2 additions & 2 deletions src/ipush/notify/chanify.py → src/ipush/provider/chanify.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from ..utils.fetch import Fetch
from .notify import Notify
from ._provider import Provider


class Chanify(Notify):
class Chanify(Provider):
"""
Chanify通知
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from ..utils.fetch import Fetch
from ..utils.helper import signature
from .notify import Notify
from ._provider import Provider


class Dingtalk(Notify):
class Dingtalk(Provider):
"""
钉钉通知
"""
Expand Down
4 changes: 2 additions & 2 deletions src/ipush/notify/feishu.py → src/ipush/provider/feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from ..utils.fetch import Fetch
from ..utils.helper import signature
from .notify import Notify
from ._provider import Provider


class Feishu(Notify):
class Feishu(Provider):
"""
飞书通知
"""
Expand Down
4 changes: 2 additions & 2 deletions src/ipush/notify/lark.py → src/ipush/provider/lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from ..utils.fetch import Fetch
from ..utils.helper import signature
from .notify import Notify
from ._provider import Provider


class Lark(Notify):
class Lark(Provider):
"""
Lark通知
"""
Expand Down
46 changes: 46 additions & 0 deletions src/ipush/provider/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import json

from ..utils.fetch import Fetch
from ._provider import Provider


class Notify(Provider):
"""
Notify通知
"""

def __init__(self, token='', user_id=''):
self.token = token
self.user_id = user_id

def _signature(self):
pass

def _geturl(self):
"""
生成请求的 URL
"""
return 'https://notify.dev/api/v1/notify'

def send(self, message, title=''):
"""
发送通知
:param message: 消息内容
"""
req_url = self._geturl()

headers = {
'Content-Type': 'application/json',
'Authorization': self.token,
}
req = Fetch()
req.update_headers(headers)

data = {
'user_id': self.user_id,
'title': '新消息' if title == '' else title,
'body': message,
}
data = json.dumps(data, indent=4)
req.post(req_url, data=data.encode('utf-8'))
return req.response
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from ..utils.fetch import Fetch
from .notify import Notify
from ._provider import Provider


class PushDeer(Notify):
class PushDeer(Provider):
"""
PushDeer通知
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from ..utils.fetch import Fetch
from .notify import Notify
from ._provider import Provider


class PushPlus(Notify):
class PushPlus(Provider):
"""
PushPlus通知
"""
Expand Down
4 changes: 2 additions & 2 deletions src/ipush/notify/showdoc.py → src/ipush/provider/showdoc.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from ..utils.fetch import Fetch
from .notify import Notify
from ._provider import Provider


class Showdoc(Notify):
class Showdoc(Provider):
"""
Showdoc通知
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from ..utils.fetch import Fetch
from .notify import Notify
from ._provider import Provider


class Telegram(Notify):
class Telegram(Provider):
"""
Telegram通知
"""
Expand Down
4 changes: 2 additions & 2 deletions src/ipush/notify/xizhi.py → src/ipush/provider/xizhi.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import json

from ..utils.fetch import Fetch
from .notify import Notify
from ._provider import Provider


class Xizhi(Notify):
class Xizhi(Provider):
"""
Xizhi通知
"""
Expand Down
16 changes: 16 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest


@pytest.fixture
def title():
return 'iPush'


@pytest.fixture
def message():
return 'PyPush test'


@pytest.fixture
def custom_message():
return 'PyPush test custom url'
35 changes: 35 additions & 0 deletions tests/test_alertzy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os

import pytest

from ipush import Alertzy


@pytest.fixture
def access_token():
token = os.environ.get('AlertzyToken')
return token


@pytest.mark.skipif(
not os.environ.get('AlertzyToken'), reason='Alertzy Token not provided'
)
def test_alertzy(access_token, message):
token = access_token
notify = Alertzy(token)
res = notify.send(message)
assert res.status_code == 200
json = res.json()
assert json['response'] == 'success'


@pytest.mark.skipif(
not os.environ.get('AlertzyToken'), reason='Alertzy Token not provided'
)
def test_alertzy_title(access_token, title, message):
token = access_token
notify = Alertzy(token)
res = notify.send(message, title)
assert res.status_code == 200
json = res.json()
assert json['response'] == 'success'
Loading

0 comments on commit df71e34

Please sign in to comment.