forked from JogleLew/channel-helper-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_global.py
116 lines (100 loc) · 3.2 KB
/
helper_global.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Channel Helper Bot """
""" helper_global.py """
""" Copyright 2018, Jogle Lew """
from threading import Lock
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
lock = Lock()
class GlobalVar:
var_set = {}
def assign(var_name, var_value):
lock.acquire()
GlobalVar.var_set[var_name] = var_value
lock.release()
def value(var_name, default_value):
lock.acquire()
if not var_name in GlobalVar.var_set:
GlobalVar.var_set[var_name] = default_value
result = GlobalVar.var_set[var_name]
lock.release()
return result
def get_sender_name(message):
real_sender = message.from_user
if message.forward_from:
real_sender = message.forward_from
username = real_sender.first_name
if real_sender.last_name:
username = username + " " + real_sender.last_name
if message.forward_from_chat:
username = message.forward_from_chat.title
return username
def records_to_str(records):
s = value("comment_header", "") + "\n"
if records is None or len(records) == 0:
s += value("comment_empty", "")
return s
records = records[::-1]
for record in records:
username = record[2]
name = record[3]
msg_type = record[4]
msg_content = record[5]
s += ("<b>%s</b>: " % name)
if not msg_type == "text":
s += ("[%s] " % msg_type)
s += msg_content + "\n"
return s
def records_to_buttons(records, channel_id, msg_id):
b = []
if records is None or len(records) == 0:
return b
records = records[::-1]
for idx, record in enumerate(records):
username = record[2]
name = record[3]
msg_type = record[4]
msg_content = record[5]
row_id = record[10]
s = ("%s: " % name)
if not msg_type == "text":
s += ("[%s] " % msg_type)
s += msg_content
button = [[
InlineKeyboardButton(
s,
callback_data="msg_detail,%d,%d,%d" % (channel_id, msg_id, row_id)
)
]]
b += button
return b
def parse_entity(src, entity_list):
if entity_list is None or len(entity_list) == 0:
return src.replace('<', '<').replace('>', '>')
head = 0
p_str = ""
for entity in entity_list:
begin_str = ''
end_str = ''
if entity.type == 'bold':
begin_str = '<b>'
end_str = '</b>'
elif entity.type == 'code':
begin_str = '<code>'
end_str = '</code>'
elif entity.type == 'italic':
begin_str = '<i>'
end_str = '</i>'
elif entity.type == 'pre':
begin_str = '<pre>'
end_str = '</pre>'
elif entity.type == 'text_link':
begin_str = '<a href="%s">' % entity.url
end_str = '</a>'
p_str += src[head:entity.offset].replace('<', '<').replace('>', '>')
p_str += begin_str
p_str += src[entity.offset:(entity.offset + entity.length)].replace('<', '<').replace('>', '>')
p_str += end_str
head = entity.offset + entity.length
p_str += src[head:]
return p_str