Skip to content

Instantly share code, notes, and snippets.

@satouriko
Created October 19, 2018 19:40
Show Gist options
  • Save satouriko/3c33ae14defd938fc38950527a7e7483 to your computer and use it in GitHub Desktop.
Save satouriko/3c33ae14defd938fc38950527a7e7483 to your computer and use it in GitHub Desktop.
CQHTTP 版本发布桥接 Bot
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ldbg_jsonrpc import client
import sys
import uuid
if __name__ == "__main__":
c = client.Client("localhost", 4700)
c.connect()
c.init_channel({
"uuid": "ac9a2d39-ba1e-4fda-9bcf-79e807fe0e0e",
"producer": True,
"consumer": True,
"accept": [{
"from": "QQ|Telegram",
"to": ".*",
"formats": [{
"api": "ubm-api",
"version": "1.0",
"method": "receive"
}]
}]
})
while True:
res, ok = c.get_updates({
"uuid": "ac9a2d39-ba1e-4fda-9bcf-79e807fe0e0e",
"timeout": "10s",
"limit": 100,
})
if ok and res['code'] == 10000:
for pkt in res['packets']:
print(pkt)
try:
if pkt['body']['message']['chat']['cid']['messenger'] == "QQ" and \
pkt['body']['message']['chat']['cid']['chat_id'] == "201865589" and \
pkt['body']['message']['chat']['cid']['chat_type'] == "group":
reply = pkt['body']
reply['message']['cid'] = {
"messenger": "Telegram",
"chat_id": "-1001246265152",
"chat_type": "channel"
}
c.send({
"uuid": "ac9a2d39-ba1e-4fda-9bcf-79e807fe0e0e",
"packet": {
"head": {
"uuid": str(uuid.uuid4()),
"from": "CQHTTP Version Bot",
"to": "Telegram",
"format": {
"api": "ubm-api",
"version": "1.0",
"method": "send"
}
},
"body": reply
}
})
except:
print("error:", sys.exc_info()[0])
try:
if pkt['body']['message']['chat']['cid']['messenger'] == "Telegram" and \
pkt['body']['message']['chat']['cid']['chat_id'] == "-1001246265152" and \
pkt['body']['message']['chat']['cid']['chat_type'] == "channel":
reply = pkt['body']
reply['message']['cid'] = {
"messenger": "QQ",
"chat_id": "201865589",
"chat_type": "group"
}
c.send({
"uuid": "ac9a2d39-ba1e-4fda-9bcf-79e807fe0e0e",
"packet": {
"head": {
"uuid": str(uuid.uuid4()),
"from": "CQHTTP Version Bot",
"to": "QQ",
"format": {
"api": "ubm-api",
"version": "1.0",
"method": "send"
}
},
"body": reply
}
})
except:
print("error:", sys.exc_info()[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment