├── .gitignore ├── ELang ├── CQPAppSdk.ec ├── com.broncotc.cqsocketapi.e ├── com.broncotc.cqsocketapi.e.txt ├── com.broncotc.cqsocketapi.json ├── cqsocketconfig.ini └── jingyi_module_v5.11.ec ├── LICENSE.txt ├── Python ├── Telegram_QQ_tunnel.py ├── botconfig.py.example ├── pycoolq │ └── __init__.py ├── requirements.txt └── sympybot.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Custom template 3 | botconfig.py 4 | libirc.py 5 | *.bak 6 | ### Windows template 7 | # Windows image file caches 8 | Thumbs.db 9 | ehthumbs.db 10 | 11 | # Folder config file 12 | Desktop.ini 13 | 14 | # Recycle Bin used on file shares 15 | $RECYCLE.BIN/ 16 | 17 | # Windows Installer files 18 | *.cab 19 | *.msi 20 | *.msm 21 | *.msp 22 | 23 | 24 | # Windows shortcuts 25 | *.lnk 26 | ### Python template 27 | # Byte-compiled / optimized / DLL files 28 | __pycache__/ 29 | *.py[cod] 30 | *$py.class 31 | 32 | # C extensions 33 | *.so 34 | 35 | # Distribution / packaging 36 | .Python 37 | env/ 38 | build/ 39 | develop-eggs/ 40 | dist/ 41 | downloads/ 42 | eggs/ 43 | .eggs/ 44 | lib/ 45 | lib64/ 46 | parts/ 47 | sdist/ 48 | var/ 49 | *.egg-info/ 50 | .installed.cfg 51 | *.egg 52 | 53 | # PyInstaller 54 | # Usually these files are written by a python script from a template 55 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 56 | *.manifest 57 | *.spec 58 | 59 | # Installer logs 60 | pip-log.txt 61 | pip-delete-this-directory.txt 62 | 63 | # Unit test / coverage reports 64 | htmlcov/ 65 | .tox/ 66 | .coverage 67 | .coverage.* 68 | .cache 69 | nosetests.xml 70 | coverage.xml 71 | *,cover 72 | 73 | # Translations 74 | *.mo 75 | *.pot 76 | 77 | # Django stuff: 78 | *.log 79 | 80 | # Sphinx documentation 81 | docs/_build/ 82 | 83 | # PyBuilder 84 | target/ 85 | ### JetBrains template 86 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 87 | 88 | *.iml 89 | 90 | ## Directory-based project format: 91 | .idea/ 92 | # if you remove the above rule, at least ignore the following: 93 | 94 | # User-specific stuff: 95 | # .idea/workspace.xml 96 | # .idea/tasks.xml 97 | # .idea/dictionaries 98 | 99 | # Sensitive or high-churn files: 100 | # .idea/dataSources.ids 101 | # .idea/dataSources.xml 102 | # .idea/sqlDataSources.xml 103 | # .idea/dynamic.xml 104 | # .idea/uiDesigner.xml 105 | 106 | # Gradle: 107 | # .idea/gradle.xml 108 | # .idea/libraries 109 | 110 | # Mongo Explorer plugin: 111 | # .idea/mongoSettings.xml 112 | 113 | ## File-based project format: 114 | *.ipr 115 | *.iws 116 | 117 | ## Plugin-specific files: 118 | 119 | # IntelliJ 120 | /out/ 121 | 122 | # mpeltonen/sbt-idea plugin 123 | .idea_modules/ 124 | 125 | # JIRA plugin 126 | atlassian-ide-plugin.xml 127 | 128 | # Crashlytics plugin (for Android Studio and IntelliJ) 129 | com_crashlytics_export_strings.xml 130 | crashlytics.properties 131 | crashlytics-build.properties 132 | 133 | -------------------------------------------------------------------------------- /ELang/CQPAppSdk.ec: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/broncotc/PySocketCoolQ/9c5f34c3bf43900b7ae43ed3885b3365e3a6a49b/ELang/CQPAppSdk.ec -------------------------------------------------------------------------------- /ELang/com.broncotc.cqsocketapi.e: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/broncotc/PySocketCoolQ/9c5f34c3bf43900b7ae43ed3885b3365e3a6a49b/ELang/com.broncotc.cqsocketapi.e -------------------------------------------------------------------------------- /ELang/com.broncotc.cqsocketapi.e.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/broncotc/PySocketCoolQ/9c5f34c3bf43900b7ae43ed3885b3365e3a6a49b/ELang/com.broncotc.cqsocketapi.e.txt -------------------------------------------------------------------------------- /ELang/com.broncotc.cqsocketapi.json: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/broncotc/PySocketCoolQ/9c5f34c3bf43900b7ae43ed3885b3365e3a6a49b/ELang/com.broncotc.cqsocketapi.json -------------------------------------------------------------------------------- /ELang/cqsocketconfig.ini: -------------------------------------------------------------------------------- 1 | [Ports] 2 | cqsend=13423 3 | cqlisten=13424 4 | -------------------------------------------------------------------------------- /ELang/jingyi_module_v5.11.ec: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/broncotc/PySocketCoolQ/9c5f34c3bf43900b7ae43ed3885b3365e3a6a49b/ELang/jingyi_module_v5.11.ec -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 BroncoTc 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Python/Telegram_QQ_tunnel.py: -------------------------------------------------------------------------------- 1 | # coding: UTF-8 2 | 3 | import sys 4 | import logging 5 | 6 | from botconfig import botconfig 7 | 8 | reload(sys) 9 | sys.setdefaultencoding('utf-8') 10 | import telebot 11 | import pycoolq 12 | 13 | telegramToken = botconfig.telegramToken 14 | 15 | tgbot = telebot.TeleBot(token=telegramToken) 16 | 17 | listenPort = int(sys.argv[1]) 18 | sendPort = int(sys.argv[2]) 19 | 20 | qqbot = pycoolq.coolqBot(py2cqPort=sendPort, cq2pyPort=listenPort) 21 | 22 | 23 | @tgbot.message_handler() 24 | def groupPassQQ(message): 25 | textContent = message.text.encode('utf-8') 26 | sentChat = message.chat.id 27 | logging.warning("Telegram Message Received") 28 | senderName = message.from_user.first_name 29 | if message.from_user.last_name: 30 | senderName += " " + message.from_user.last_name 31 | 32 | if sentChat == botconfig.telegramGroupID: 33 | sendMessage = pycoolq.sendMessage("group", botconfig.qqGroupID, "[%s] %s" % (senderName, textContent)) 34 | qqbot.send(sendMessage) 35 | 36 | 37 | @qqbot.qqMessageHandler() 38 | def pass2TG(message): 39 | logging.warning(message.sourceType + " " + str(message.fromGroupID) + " " + message.content) 40 | if message.sourceType == "group" and message.fromGroupID == botconfig.qqGroupID: 41 | msg = message.content 42 | sender = message.fromID 43 | tgbot.send_message(chat_id=botconfig.telegramGroupID, text="[%s] %s" % (sender, msg)) 44 | 45 | 46 | qqbot.startListen() 47 | tgbot.polling(none_stop=True) 48 | -------------------------------------------------------------------------------- /Python/botconfig.py.example: -------------------------------------------------------------------------------- 1 | # coding: UTF-8 2 | # EDIT THIS FILE TO FIT YOUR USAGE 3 | # RENAME IT TO botconfig.py 4 | 5 | 6 | class botconfig(): 7 | telegramToken = "123456789:AAabcdef4VMp7rsdfkelskdfkslevYhbQs" 8 | qqGroupID = 112345672 9 | telegramGroupID = -1123012345672 10 | -------------------------------------------------------------------------------- /Python/pycoolq/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: UTF-8 2 | import re 3 | import sys 4 | import threading 5 | import socket 6 | import collections 7 | from urllib import quote, unquote 8 | from circuits import handler 9 | from circuits.net.sockets import TCPServer 10 | 11 | reload(sys) 12 | sys.setdefaultencoding('utf-8') 13 | 14 | receivedMessage = collections.namedtuple('receivedMessage', 15 | ('sourceType', 'fromGroupID', 'fromID', 'content')) 16 | 17 | sendMessage = collections.namedtuple('sendMessage', 18 | ('destinationType', 'destinationID', 'content')) 19 | 20 | re_cq_special = re.compile(r'\[CQ:(face|emoji|at|shake|music|anonymous|image|record)(,\w+=[^]]+)?\]') 21 | 22 | class messagePreprocessor(TCPServer): 23 | @handler("read") 24 | def on_read(self, sock, data): 25 | self.qqMessageHandlerExecutor(data) 26 | return 0 27 | 28 | def setMessageHandlers(self, handlers): 29 | self.messageHandlers = handlers 30 | 31 | def qqMessageHandlerExecutor(self, data): 32 | string = str(data) 33 | dataParts = string.split(" ") 34 | if dataParts[0] in ("group", "discuss"): 35 | msg = unquote(dataParts[3]).decode("gbk") 36 | sender = int(dataParts[2]) 37 | sendGroup = int(dataParts[1]) 38 | message = receivedMessage(sourceType=dataParts[0], fromGroupID=sendGroup, fromID=sender, content=msg) 39 | elif dataParts[0] == 'personal': 40 | msg = unquote(dataParts[2]).decode("gbk") 41 | sender = int(dataParts[1]) 42 | message = receivedMessage(sourceType=dataParts[0], fromGroupID=0, fromID=sender, content=msg) 43 | for i in self.messageHandlers: 44 | i(message) 45 | 46 | class coolqBot(): 47 | def __init__(self, py2cqPort, cq2pyPort): 48 | self.sendPort = py2cqPort 49 | self.receivePort = cq2pyPort 50 | self.messageHandlers = [] 51 | 52 | self.sock = socket.socket(type=socket.SOCK_DGRAM) 53 | self.sock.connect(("127.0.0.1", self.sendPort)) 54 | self.listener = messagePreprocessor(("0.0.0.0", self.receivePort)) 55 | self.listener.setMessageHandlers(self.messageHandlers) 56 | 57 | def send(self, sendMessage): 58 | self.sock.send(sendMessage.destinationType + " " + str(sendMessage.destinationID) + " " + quote(sendMessage.content.encode('utf-8'))) 59 | return 0 60 | 61 | def listenerStarter(self): 62 | self.listener.start() 63 | 64 | def startListen(self): 65 | poll = threading.Thread(target=self.listenerStarter) 66 | poll.setDaemon(True) 67 | poll.start() 68 | 69 | def qqMessageHandler(self): 70 | def decorator(handler): 71 | self.messageHandlers.append(handler) 72 | return handler 73 | 74 | return decorator 75 | -------------------------------------------------------------------------------- /Python/requirements.txt: -------------------------------------------------------------------------------- 1 | pyTelegramBotAPI 2 | circuits 3 | -------------------------------------------------------------------------------- /Python/sympybot.py: -------------------------------------------------------------------------------- 1 | # coding : UTF-8 2 | from __future__ import division 3 | from sympy import * 4 | x, y, z, t = symbols('x y z t') 5 | k, m, n = symbols('k m n', integer=True) 6 | f, g, h = symbols('f g h', cls=Function) 7 | import sys 8 | import logging 9 | import pycoolq 10 | 11 | listenPort = int(sys.argv[1]) 12 | sendPort = int(sys.argv[2]) 13 | 14 | 15 | qqbot = pycoolq.coolqBot(py2cqPort=sendPort, cq2pyPort=listenPort) 16 | 17 | 18 | @qqbot.qqMessageHandler() 19 | def pass2TG(message): 20 | logging.warning(message.sourceType + " " + str(message.fromGroupID) + " " + message.content) 21 | if message.sourceType == "group" and message.fromGroupID == botconfig.qqGroupID: 22 | msg = message.content 23 | sender = message.fromID 24 | if sentChat == botconfig.telegramGroupID: 25 | sendMessage = pycoolq.sendMessage("group", botconfig.qqGroupID, "[%s] %s" % (senderName, textContent)) 26 | qqbot.send(sendMessage) 27 | 28 | 29 | 30 | qqbot.startListen() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##Socket API and Python Library for CoolQ bot Platform 2 | 3 | 4 | ####This is repository is under heavy development, DO NOT USE IT IN PRODUCTION 5 | > 6 | 7 | 8 | *.e files are souce code for ELang (the Chinese programming language used by CoolQ) 9 | 10 | The JSON file in ELang folder is encoded in GBK. 11 | 12 | botconfig.py should be created to be used with the exemplary QQ & Telegram tunnelling bot. 13 | 14 | Python side is recommended to be started after the initialization of Elang side. 15 | 16 | The setup.py file might be crappy, but the library itself is usable. --------------------------------------------------------------------------------