Compare commits
No commits in common. "10af1ca2ad639cde7abbc37e2225477fb2e01202" and "1cae6c17df3fc06df734c40f4e5ca65c08da040e" have entirely different histories.
10af1ca2ad
...
1cae6c17df
22
README.md
22
README.md
@ -1,22 +1,2 @@
|
||||
# imgateway
|
||||
|
||||
# IM Platform Middleware
|
||||
|
||||
Enterprise IM middleware supporting:
|
||||
|
||||
- Feishu
|
||||
- WeCom
|
||||
- Multi-tenant
|
||||
- Organization binding
|
||||
- Webhook gateway
|
||||
- Adapter architecture
|
||||
|
||||
## Start
|
||||
|
||||
pip install -r requirements.txt
|
||||
|
||||
python -m im_platform.server
|
||||
|
||||
## Webhooks
|
||||
|
||||
POST /webhook/feishu
|
||||
POST /webhook/wecom
|
||||
|
||||
@ -1,16 +0,0 @@
|
||||
|
||||
[
|
||||
{
|
||||
"org_id":"org_demo",
|
||||
"platform":"feishu",
|
||||
"app_id":"cli_xxx",
|
||||
"app_secret":"xxx"
|
||||
},
|
||||
{
|
||||
"org_id":"org_demo2",
|
||||
"platform":"wecom",
|
||||
"corp_id":"wwxxx",
|
||||
"agent_id":"1000002",
|
||||
"secret":"xxx"
|
||||
}
|
||||
]
|
||||
@ -1,8 +0,0 @@
|
||||
|
||||
class BaseAdapter:
|
||||
|
||||
def __init__(self, tenant):
|
||||
self.tenant = tenant
|
||||
|
||||
async def send_text(self, user, text):
|
||||
raise NotImplementedError()
|
||||
@ -1,19 +0,0 @@
|
||||
|
||||
import aiohttp
|
||||
from im_platform.adapters.base import BaseAdapter
|
||||
|
||||
class FeishuAdapter(BaseAdapter):
|
||||
|
||||
async def send_text(self, user, text):
|
||||
|
||||
url = "https://open.feishu.cn/open-apis/im/v1/messages"
|
||||
|
||||
payload = {
|
||||
"receive_id": user,
|
||||
"msg_type": "text",
|
||||
"content": {"text": text}
|
||||
}
|
||||
|
||||
async with aiohttp.ClientSession() as s:
|
||||
async with s.post(url, json=payload) as r:
|
||||
return await r.text()
|
||||
@ -1,20 +0,0 @@
|
||||
|
||||
import aiohttp
|
||||
from im_platform.adapters.base import BaseAdapter
|
||||
|
||||
class WeComAdapter(BaseAdapter):
|
||||
|
||||
async def send_text(self, user, text):
|
||||
|
||||
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send"
|
||||
|
||||
payload = {
|
||||
"touser": user,
|
||||
"msgtype": "text",
|
||||
"agentid": self.tenant["agent_id"],
|
||||
"text": {"content": text}
|
||||
}
|
||||
|
||||
async with aiohttp.ClientSession() as s:
|
||||
async with s.post(url, json=payload) as r:
|
||||
return await r.text()
|
||||
@ -1,17 +0,0 @@
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
async def feishu_webhook(request):
|
||||
|
||||
data = await request.json()
|
||||
print("feishu event", data)
|
||||
|
||||
return web.json_response({"ok": True})
|
||||
|
||||
|
||||
async def wecom_webhook(request):
|
||||
|
||||
data = await request.json()
|
||||
print("wecom event", data)
|
||||
|
||||
return web.json_response({"ok": True})
|
||||
@ -1,24 +0,0 @@
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
TENANT_CACHE = {}
|
||||
|
||||
async def load_tenants():
|
||||
global TENANT_CACHE
|
||||
p = Path(__file__).resolve().parent.parent / "config" / "tenants.json"
|
||||
if p.exists():
|
||||
TENANT_CACHE = json.loads(p.read_text())
|
||||
return TENANT_CACHE
|
||||
|
||||
def get_tenant_by_org(org_id):
|
||||
for t in TENANT_CACHE:
|
||||
if t["org_id"] == org_id:
|
||||
return t
|
||||
return None
|
||||
|
||||
def get_tenant_by_platform(platform, key):
|
||||
for t in TENANT_CACHE:
|
||||
if t["platform"] == platform and (t.get("app_id")==key or t.get("corp_id")==key):
|
||||
return t
|
||||
return None
|
||||
@ -1,17 +0,0 @@
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
CACHE = {}
|
||||
|
||||
async def get_token(key):
|
||||
v = CACHE.get(key)
|
||||
if not v:
|
||||
return None
|
||||
token, expire = v
|
||||
if expire < time.time():
|
||||
return None
|
||||
return token
|
||||
|
||||
async def set_token(key, token, ttl):
|
||||
CACHE[key] = (token, time.time() + ttl - 60)
|
||||
@ -1,63 +0,0 @@
|
||||
{
|
||||
"summary": [
|
||||
{
|
||||
"name": "im_tenant",
|
||||
"title": "IM\u79df\u6237",
|
||||
"primary": "id",
|
||||
"catelog": "relation"
|
||||
}
|
||||
],
|
||||
"fields": [
|
||||
{
|
||||
"name": "id",
|
||||
"title": "ID",
|
||||
"type": "str",
|
||||
"length": 32
|
||||
},
|
||||
{
|
||||
"name": "org_id",
|
||||
"title": "\u7ec4\u7ec7ID",
|
||||
"type": "str",
|
||||
"length": 32
|
||||
},
|
||||
{
|
||||
"name": "platform",
|
||||
"title": "\u5e73\u53f0",
|
||||
"type": "str",
|
||||
"length": 16
|
||||
},
|
||||
{
|
||||
"name": "app_id",
|
||||
"title": "APPID",
|
||||
"type": "str",
|
||||
"length": 64
|
||||
},
|
||||
{
|
||||
"name": "app_secret",
|
||||
"title": "APPSECRET",
|
||||
"type": "str",
|
||||
"length": 128
|
||||
},
|
||||
{
|
||||
"name": "corp_id",
|
||||
"title": "\u4f01\u4e1aID",
|
||||
"type": "str",
|
||||
"length": 64
|
||||
},
|
||||
{
|
||||
"name": "agent_id",
|
||||
"title": "AgentID",
|
||||
"type": "str",
|
||||
"length": 32
|
||||
}
|
||||
],
|
||||
"indexes": [
|
||||
{
|
||||
"name": "idx_org",
|
||||
"idxtype": "index",
|
||||
"idxfields": [
|
||||
"org_id"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
|
||||
from aiohttp import web
|
||||
from im_platform.api.webhooks import feishu_webhook, wecom_webhook
|
||||
|
||||
async def init_app():
|
||||
app = web.Application()
|
||||
|
||||
app.router.add_post("/webhook/feishu", feishu_webhook)
|
||||
app.router.add_post("/webhook/wecom", wecom_webhook)
|
||||
|
||||
return app
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
app = asyncio.run(init_app())
|
||||
web.run_app(app, port=8080)
|
||||
@ -1,13 +0,0 @@
|
||||
|
||||
import asyncio
|
||||
|
||||
QUEUE = asyncio.Queue()
|
||||
|
||||
async def push_event(event):
|
||||
await QUEUE.put(event)
|
||||
|
||||
async def worker():
|
||||
|
||||
while True:
|
||||
event = await QUEUE.get()
|
||||
print("process event", event)
|
||||
@ -1,9 +0,0 @@
|
||||
|
||||
[project]
|
||||
name = "im_platform"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"ahserver",
|
||||
"cryptography",
|
||||
"redis"
|
||||
]
|
||||
Loading…
x
Reference in New Issue
Block a user