Skip to content

Commit fa30263

Browse files
committed
feat: add telegram
1 parent cade62a commit fa30263

File tree

16 files changed

+137
-20
lines changed

16 files changed

+137
-20
lines changed
File renamed without changes.

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
APP 推送通知。支持往 **钉钉群、飞书群、Lark 群、Bark、Chanify、PushDeer、PushPlus、Showdoc、息知** 推送消息。
44

5+
---
6+
57
<a href="https://pypi.org/project/ipush" target="_blank">
68
<img src="https://img.shields.io/pypi/v/ipush.svg" alt="Package version">
79
</a>
@@ -21,7 +23,7 @@ pip install -U ipush
2123
2. 创建 `notify` 对象,并发送消息
2224

2325
```python
24-
from ipush.notify.dingtalk import Dingtalk
26+
from ipush import Dingtalk
2527

2628
notify = Dingtalk("token", "secret")
2729
notify.send("ipush test")
@@ -42,6 +44,10 @@ notify.send("ipush test")
4244
|| **Showdoc** | [https://push.showdoc.com.cn/](https://www.showdoc.com.cn/push) | - | |
4345
|| **息知** | [https://xz.qqoq.net/](https://xz.qqoq.net/) | - | |
4446

47+
| 状态 | **国外**平台 | 官网 | 文档 | 备注 |
48+
| :------- | :----------- | :-------------------------------------------------------------------------------- | :--- | :--------------------------------------------------------------------------------------------------------------------------------------------------- |
49+
|**** | **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` |
50+
4551
## 开发
4652

4753
### 1. 前置开发环境

docs/README.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pip install ipush
1313
- **钉钉群**
1414

1515
```python
16-
from ipush.notify.dingtalk import Dingtalk
16+
from ipush import Dingtalk
1717

1818
notify = Dingtalk("token", "secret")
1919
notify.send("ipush test")
@@ -22,7 +22,7 @@ notify.send("ipush test")
2222
- **飞书群**
2323

2424
```python
25-
from ipush.notify.feishu import Feishu
25+
from ipush import Feishu
2626

2727
notify = Feishu("token", "secret")
2828
notify.send("ipush test")
@@ -31,7 +31,7 @@ notify.send("ipush test")
3131
- **Lark 群**
3232

3333
```python
34-
from ipush.notify.lark import Lark
34+
from ipush import Lark
3535

3636
notify = Lark("token", "secret")
3737
notify.send("ipush test")
@@ -40,7 +40,7 @@ notify.send("ipush test")
4040
- **Bark**
4141

4242
```python
43-
from ipush.notify.bark import Bark
43+
from ipush import Bark
4444

4545
notify = Bark("token")
4646
notify.send("ipush test", "title")
@@ -51,7 +51,7 @@ notify.send("ipush test custom url")
5151
- **Chanify**
5252

5353
```python
54-
from ipush.notify.chanify import Chanify
54+
from ipush import Chanify
5555

5656
notify = Chanify("token")
5757
notify.send("ipush test")
@@ -62,7 +62,7 @@ notify.send("ipush test custom url")
6262
- **PushDeer**
6363

6464
```python
65-
from ipush.notify.pushdeer import PushDeer
65+
from ipush import PushDeer
6666

6767
notify = PushDeer("token")
6868
notify.send("ipush test")
@@ -73,7 +73,7 @@ notify.send("ipush test custom url")
7373
- **PushPlus**
7474

7575
```python
76-
from ipush.notify.pushplus import PushPlus
76+
from ipush import PushPlus
7777

7878
notify = PushPlus("token")
7979
notify.send("ipush test", "title")
@@ -82,7 +82,7 @@ notify.send("ipush test", "title")
8282
- **Showdoc**
8383

8484
```python
85-
from ipush.notify.showdoc import Showdoc
85+
from ipush import Showdoc
8686

8787
notify = Showdoc("token")
8888
notify.send("ipush test", "title")
@@ -91,8 +91,19 @@ notify.send("ipush test", "title")
9191
- **Xizhi**
9292

9393
```python
94-
from ipush.notify.xizhi import Xizhi
94+
from ipush import Xizhi
9595

9696
notify = Xizhi("token")
9797
notify.send("ipush test", "title")
9898
```
99+
100+
- **Telegram**
101+
102+
```python
103+
from ipush import Telegram
104+
105+
notify = Telegram("token")
106+
notify.send("ipush test", "chat_id")
107+
notify.seturl("https://self-hosted")
108+
notify.send("ipush test custom url")
109+
```

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fix = true
7474
show-fixes = true
7575
show-source = true
7676
# line-length = 100
77-
ignore = ["E501"]
77+
ignore = ["E501","F401"]
7878

7979
[tool.ruff.format]
8080
quote-style = "single"

src/ipush/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from .notify.bark import Bark
2+
from .notify.chanify import Chanify
3+
from .notify.dingtalk import Dingtalk
4+
from .notify.feishu import Feishu
5+
from .notify.lark import Lark
6+
from .notify.pushdeer import PushDeer
7+
from .notify.pushplus import PushPlus
8+
from .notify.showdoc import Showdoc
9+
from .notify.telegram import Telegram
10+
from .notify.xizhi import Xizhi

src/ipush/notify/telegram.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import json
2+
3+
from ..utils.fetch import Fetch
4+
from .notify import Notify
5+
6+
7+
class Telegram(Notify):
8+
"""
9+
Telegram通知
10+
"""
11+
12+
def __init__(self, token='', chatid=''):
13+
self.token = token
14+
self.chatid = chatid
15+
self.url = 'https://api.telegram.org'
16+
17+
def _signature(self):
18+
pass
19+
20+
def seturl(self, url):
21+
self.url = url
22+
23+
def _geturl(self, uri='sendMessage'):
24+
"""
25+
生成请求的 URL
26+
"""
27+
return f'{self.url}/bot{self.token}/{uri}'
28+
29+
def send(self, message):
30+
"""
31+
发送通知
32+
:param message: 消息内容
33+
"""
34+
req_url = self._geturl()
35+
36+
headers = {
37+
'content-type': 'application/json',
38+
}
39+
req = Fetch()
40+
req.update_headers(headers)
41+
42+
data = {
43+
'chat_id': self.chatid,
44+
'text': message,
45+
}
46+
data = json.dumps(data, indent=4)
47+
req.post(req_url, data=data.encode('utf-8'))
48+
return req.response

tests/test_bark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from ipush.notify.bark import Bark
5+
from ipush import Bark
66

77

88
@pytest.fixture

tests/test_chanify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from ipush.notify.chanify import Chanify
5+
from ipush import Chanify
66

77

88
@pytest.fixture

tests/test_dingtalk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from ipush.notify.dingtalk import Dingtalk
5+
from ipush import Dingtalk
66

77

88
@pytest.fixture

tests/test_feishu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from ipush.notify.feishu import Feishu
5+
from ipush import Feishu
66

77

88
@pytest.fixture

0 commit comments

Comments
 (0)