',
60 | EntityType.CODE_BLOCK: '',
61 | EntityType.UNDERLINE: '',
62 | EntityType.STRIKETHROUGH: '',
63 | EntityType.QUOTE: '',
64 | EntityType.QUOTE_BLOCK: '',
65 | EntityType.LINK: '',
66 | }
67 |
68 | entity_end = {
69 | EntityType.PLAIN: '',
70 | EntityType.BOLD: '
',
71 | EntityType.ITALIC: '',
72 | EntityType.CODE: '',
73 | EntityType.CODE_BLOCK: '',
74 | EntityType.UNDERLINE: '',
75 | EntityType.STRIKETHROUGH: '',
76 | EntityType.QUOTE: '',
77 | EntityType.QUOTE_BLOCK: '',
78 | EntityType.LINK: '',
79 | }
80 | else:
81 | entity_start = {
82 | EntityType.PLAIN: '',
83 | EntityType.BOLD: '**\u200b',
84 | EntityType.ITALIC: '*\u200b',
85 | EntityType.CODE: '`\u200b',
86 | EntityType.CODE_BLOCK: '```\u200b',
87 | EntityType.UNDERLINE: '__\u200b',
88 | EntityType.STRIKETHROUGH: '~~\u200b',
89 | EntityType.QUOTE: '> \u200b',
90 | EntityType.QUOTE_BLOCK: '>>> \u200b',
91 | EntityType.LINK: '[Link]({}) ',
92 | }
93 |
94 | entity_end = {
95 | EntityType.PLAIN: '',
96 | EntityType.BOLD: '**\u200b',
97 | EntityType.ITALIC: '*\u200b',
98 | EntityType.CODE: '`\u200b',
99 | EntityType.CODE_BLOCK: '```\u200b',
100 | EntityType.UNDERLINE: '__\u200b',
101 | EntityType.STRIKETHROUGH: '~~\u200b',
102 | EntityType.QUOTE: '',
103 | EntityType.QUOTE_BLOCK: '',
104 | EntityType.LINK: '',
105 | }
106 | escape_function = escape_markdown
107 |
108 | if not message.text_entities:
109 | return escape_function(message.text)
110 |
111 | stack: List[MessageEntity] = list()
112 | result = ''
113 | offset = 0
114 | for entity in message.text_entities:
115 | while stack and entity.start > stack[-1].end:
116 | _entity = stack[-1]
117 | if offset < _entity.end:
118 | result += escape_function(message.text[offset:_entity.end])
119 | if support_entities & _entity.entity_type:
120 | result += entity_end[_entity.entity_type]
121 | offset = _entity.end
122 | stack.pop()
123 |
124 | if entity.start > offset:
125 | result += escape_function(message.text[offset:entity.start])
126 | offset = entity.start
127 |
128 | if entity.entity_type == EntityType.LINK:
129 | if support_entities & entity.entity_type:
130 | result += entity_start[entity.entity_type].format(entity.link)
131 | else:
132 | result += f'link: {entity.link} title: '
133 | else:
134 | if support_entities & entity.entity_type:
135 | result += entity_start[entity.entity_type]
136 | stack.append(entity)
137 |
138 | while stack:
139 | _entity = stack[-1]
140 | if offset < _entity.end:
141 | result += escape_function(message.text[offset:_entity.end])
142 | if support_entities & _entity.entity_type:
143 | result += entity_end[_entity.entity_type]
144 | offset = _entity.end
145 | stack.pop()
146 |
147 | if offset < len(message.text):
148 | result += escape_function(message.text[offset:])
149 |
150 | return result
151 |
152 |
153 | def unparse_entities_to_html(message: UnifiedMessage, support_entities: EntityType):
154 | """
155 | parse plain text with entities to html
156 | :param message:
157 | :param support_entities:
158 | :return:
159 | """
160 | return unparse_entities(message, support_entities, to_type='html')
161 |
162 |
163 | def unparse_entities_to_markdown(message: UnifiedMessage, support_entities: EntityType):
164 | """
165 | parse plain text with entities to markdown
166 | :param message:
167 | :param support_entities:
168 | :return:
169 | """
170 | return unparse_entities(message, support_entities, to_type='markdown')
171 |
--------------------------------------------------------------------------------
/unified_message_relay/Util/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JQ-Networks/UnifiedMessageRelay/164af3929dd2f2df6debd72df9cf4be5af005004/unified_message_relay/Util/__init__.py
--------------------------------------------------------------------------------
/unified_message_relay/__init__.py:
--------------------------------------------------------------------------------
1 | from . import Core
2 | from . import Lib
3 | from . import Util
4 | from . import daemon
5 | __VERSION__ = '4.2'
6 |
--------------------------------------------------------------------------------
/unified_message_relay/daemon.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | # -*- coding: utf-8 -*-
3 | import argparse
4 | from .Lib.DaemonClass import Daemon
5 |
6 |
7 | class MainProcess(Daemon):
8 | def run(self, debug_mode):
9 | from .Core.UMRManager import UMRManager
10 | UMRManager.run()
11 |
12 |
13 | def main():
14 | # ARGS
15 | argP = argparse.ArgumentParser(
16 | description="QQ <-> Telegram Bot Framework & Forwarder", formatter_class=argparse.RawTextHelpFormatter)
17 | cmdHelpStr = """
18 | start - start bot as a daemon
19 | stop - stop bot
20 | restart - restart bot
21 | run - run as foreground Debug mode. every log will print to screen and log to file.
22 | """
23 | argP.add_argument("command", type=str, action="store",
24 | choices=['start', 'stop', 'restart', 'run'], help=cmdHelpStr)
25 | daemon = MainProcess('/tmp/coolq-telegram-bot.pid')
26 | args = argP.parse_args()
27 | if args.command == 'start':
28 | daemon.start(debug_mode=True)
29 | elif args.command == 'stop':
30 | daemon.stop()
31 | elif args.command == 'restart':
32 | daemon.restart(debug_mode=True)
33 | elif args.command == 'run':
34 | # Run as foreground mode
35 | daemon.run(debug_mode=True)
36 |
37 |
38 | if __name__ == '__main__':
39 | main()
40 |
--------------------------------------------------------------------------------
/unified_message_relay/test/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JQ-Networks/UnifiedMessageRelay/164af3929dd2f2df6debd72df9cf4be5af005004/unified_message_relay/test/__init__.py
--------------------------------------------------------------------------------