Skip to content

Commit

Permalink
feat: add telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
jetsung committed Dec 29, 2023
1 parent cade62a commit fa30263
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 20 deletions.
File renamed without changes.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

---

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

```python
from ipush.notify.dingtalk import Dingtalk
from ipush import Dingtalk

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

| 状态 | **国外**平台 | 官网 | 文档 | 备注 |
| :------- | :----------- | :-------------------------------------------------------------------------------- | :--- | :--------------------------------------------------------------------------------------------------------------------------------------------------- |
|**** | **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` |

## 开发

### 1. 前置开发环境
Expand Down
29 changes: 20 additions & 9 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pip install ipush
- **钉钉群**

```python
from ipush.notify.dingtalk import Dingtalk
from ipush import Dingtalk

notify = Dingtalk("token", "secret")
notify.send("ipush test")
Expand All @@ -22,7 +22,7 @@ notify.send("ipush test")
- **飞书群**

```python
from ipush.notify.feishu import Feishu
from ipush import Feishu

notify = Feishu("token", "secret")
notify.send("ipush test")
Expand All @@ -31,7 +31,7 @@ notify.send("ipush test")
- **Lark 群**

```python
from ipush.notify.lark import Lark
from ipush import Lark

notify = Lark("token", "secret")
notify.send("ipush test")
Expand All @@ -40,7 +40,7 @@ notify.send("ipush test")
- **Bark**

```python
from ipush.notify.bark import Bark
from ipush import Bark

notify = Bark("token")
notify.send("ipush test", "title")
Expand All @@ -51,7 +51,7 @@ notify.send("ipush test custom url")
- **Chanify**

```python
from ipush.notify.chanify import Chanify
from ipush import Chanify

notify = Chanify("token")
notify.send("ipush test")
Expand All @@ -62,7 +62,7 @@ notify.send("ipush test custom url")
- **PushDeer**

```python
from ipush.notify.pushdeer import PushDeer
from ipush import PushDeer

notify = PushDeer("token")
notify.send("ipush test")
Expand All @@ -73,7 +73,7 @@ notify.send("ipush test custom url")
- **PushPlus**

```python
from ipush.notify.pushplus import PushPlus
from ipush import PushPlus

notify = PushPlus("token")
notify.send("ipush test", "title")
Expand All @@ -82,7 +82,7 @@ notify.send("ipush test", "title")
- **Showdoc**

```python
from ipush.notify.showdoc import Showdoc
from ipush import Showdoc

notify = Showdoc("token")
notify.send("ipush test", "title")
Expand All @@ -91,8 +91,19 @@ notify.send("ipush test", "title")
- **Xizhi**

```python
from ipush.notify.xizhi import Xizhi
from ipush import Xizhi

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

- **Telegram**

```python
from ipush import Telegram

notify = Telegram("token")
notify.send("ipush test", "chat_id")
notify.seturl("https://self-hosted")
notify.send("ipush test custom url")
```
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fix = true
show-fixes = true
show-source = true
# line-length = 100
ignore = ["E501"]
ignore = ["E501","F401"]

[tool.ruff.format]
quote-style = "single"
Expand Down
10 changes: 10 additions & 0 deletions src/ipush/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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
48 changes: 48 additions & 0 deletions src/ipush/notify/telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json

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


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

def __init__(self, token='', chatid=''):
self.token = token
self.chatid = chatid
self.url = 'https://api.telegram.org'

def _signature(self):
pass

def seturl(self, url):
self.url = url

def _geturl(self, uri='sendMessage'):
"""
生成请求的 URL
"""
return f'{self.url}/bot{self.token}/{uri}'

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

headers = {
'content-type': 'application/json',
}
req = Fetch()
req.update_headers(headers)

data = {
'chat_id': self.chatid,
'text': message,
}
data = json.dumps(data, indent=4)
req.post(req_url, data=data.encode('utf-8'))
return req.response
2 changes: 1 addition & 1 deletion tests/test_bark.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ipush.notify.bark import Bark
from ipush import Bark


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_chanify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ipush.notify.chanify import Chanify
from ipush import Chanify


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dingtalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ipush.notify.dingtalk import Dingtalk
from ipush import Dingtalk


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ipush.notify.feishu import Feishu
from ipush import Feishu


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ipush.notify.lark import Lark
from ipush import Lark


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pushdeer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ipush.notify.pushdeer import PushDeer
from ipush import PushDeer


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pushplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ipush.notify.pushplus import PushPlus
from ipush import PushPlus


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion tests/test_showdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ipush.notify.showdoc import Showdoc
from ipush import Showdoc


@pytest.fixture
Expand Down
42 changes: 42 additions & 0 deletions tests/test_telegram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os

import pytest

from ipush import Telegram


@pytest.fixture
def access_token():
token = os.environ.get('TelegramToken')
chat_id = os.environ.get('TelegramChatId')
custom_url = os.environ.get('TelegramCustomURL')
return token, chat_id, custom_url


@pytest.mark.skipif(
not os.environ.get('TelegramToken') or not os.environ.get('TelegramChatId'),
reason='Telegram Token not provided',
)
def test_telegram(access_token):
token, chat_id, _ = access_token
notify = Telegram(token, chat_id)
res = notify.send('pypush test')
assert res.status_code == 200
json = res.json()
assert json['ok']


@pytest.mark.skipif(
not os.environ.get('TelegramToken')
or not os.environ.get('TelegramChatId')
or not os.environ.get('TelegramCustomURL'),
reason='Telegram Token not provided',
)
def test_telegram_custom_url(access_token):
token, chat_id, custom_url = access_token
notify = Telegram(token, chat_id)
notify.seturl(custom_url)
res = notify.send('pypush test custom url')
assert res.status_code == 200
json = res.json()
assert json['ok']
2 changes: 1 addition & 1 deletion tests/test_xizhi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from ipush.notify.xizhi import Xizhi
from ipush import Xizhi


@pytest.fixture
Expand Down

0 comments on commit fa30263

Please sign in to comment.