Skip to content
This repository was archived by the owner on May 17, 2022. It is now read-only.

Commit 4f5d299

Browse files
committed
bug fixed and new configuration for proxy
BugReporter now is static
1 parent 87aa9ef commit 4f5d299

File tree

5 files changed

+56
-31
lines changed

5 files changed

+56
-31
lines changed

BugReporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,6 @@ def index(self):
185185
return res
186186

187187
@cherrypy.expose
188-
@cherrypy.tools.json_out
188+
@cherrypy.tools.json_out()
189189
def json(self):
190190
return get_data

Handlers.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
DispatcherDecorators, HandlerDecorator, auth, MessageHandlerDecorator)
2424
from main import BotHandler
2525

26+
# pylint: disable=unused-variable
2627

2728
def add_owner_handlers(server: BotHandler):
2829

@@ -93,7 +94,7 @@ def unknown_command(u: Update, c: CallbackContext):
9394

9495
dispatcher_decorators = DispatcherDecorators(server.dispatcher)
9596

96-
@dispatcher_decorators.messageHandler(filters=Filters.update, group=0)
97+
@dispatcher_decorators.messageHandler(Filters.update, group=0)
9798
def log_update(u: Update, c: CallbackContext):
9899
message = (
99100
'Received a new update event from telegram\n'
@@ -111,7 +112,7 @@ def log_update(u: Update, c: CallbackContext):
111112
ownerID=server.ownerID, message=html.escape(message))
112113

113114
@dispatcher_decorators.commandHandler
114-
@auth(server.ownerID, server.unknown)
115+
@auth(server.ownerID, unknown_command)
115116
def log_updates(u: Update, c: CallbackContext):
116117
server.debug = not server.debug
117118
if server.debug:
@@ -174,13 +175,13 @@ def listchats(u: Update, c: CallbackContext):
174175
u.message.reply_html(res)
175176

176177
@dispatcher_decorators.commandHandler
177-
@auth
178+
@admin_auth
178179
def send_feed_toall(u: Update, c: CallbackContext):
179180
server.send_feed(
180181
*server.read_feed(), server.get_string('last-feed'), server.iter_all_chats())
181182

182183
@dispatcher_decorators.commandHandler
183-
@auth
184+
@admin_auth
184185
def set_interval(u: Update, c: CallbackContext):
185186
if len(c.args) == 1:
186187
if c.args[0].isdigit():
@@ -226,7 +227,7 @@ def sendall(u: Update, c: CallbackContext):
226227

227228
return STATE_ADD
228229

229-
sendall_conv_handler = ConversationDecorator(sendall, per_user=True)
230+
sendall_conv_handler = ConversationDecorator([sendall], per_user=True)
230231

231232
@sendall_conv_handler.state(STATE_ADD)
232233
@MessageHandlerDecorator(Filters.regex("^✅Send$"))
@@ -719,7 +720,7 @@ def send(u: Update, c: CallbackContext):
719720
CallbackQueryHandler(cancel(STATE_CONFIRM), pattern='^no$')
720721
)
721722

722-
server.dispatcher.add_handler(sendall_conv_handler.get_handler())
723+
server.dispatcher.add_handler(sendall_conv_handler.get_handler(), group = 1)
723724

724725
def add_users_handlers(server: BotHandler):
725726
def unknown_msg(u: Update, c: CallbackContext):

config-example.conf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44
# in telegram use @botfather to create a bot and then paste token here
55
token = your bot token
66

7+
# Working behind a proxy. you can use SOCKS or HTTP proxies.
8+
# username and password is optional, if you need authentication.
9+
#
10+
# use-proxy = true
11+
#
12+
# http proxy:
13+
# proxy-url = http://USERNAME:PASSWORD@PROXY_HOST:PROXY_PORT/
14+
#
15+
# SOCKS5 proxy:
16+
# proxy-url = socks5h://URL_OF_THE_PROXY_SERVER:PROXY_PORT
17+
# proxy-user = USERNAME
18+
# proxy-pass = PASSWORD
19+
#
20+
# if you want to use socks5 I recommend you to start proxy-url with `socks5h` instead of `socks5`
21+
722
source = https://pcworms.blog.ir/rss/
823
language = en-us
924
strings-file = Default-strings.json

decorators.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ def decorator_message(func):
4444

4545
def CommandHandlerDecorator(_func=None, command=None, *args, **kwargs):
4646
def decorator_command(func):
47-
if not isinstance(command,str):
48-
command = func.__name__
49-
return CommandHandler(command,func,*args,**kwargs)
47+
command_ = command
48+
if not isinstance(command_,str):
49+
command_ = func.__name__
50+
return CommandHandler(command_,func,*args,**kwargs)
5051

5152
if _func:
5253
return decorator_command(_func)
@@ -59,11 +60,12 @@ def __init__(self, dispatcher:Dispatcher):
5960

6061
def commandHandler(self, _func=None, command=None, group=1, *args, **kwargs):
6162
def decorator_command(func):
62-
if not isinstance(command,str):
63-
command = func.__name__
64-
logging.debug(f'add command handler. command:{command} => {func}')
63+
command_ = command
64+
if not isinstance(command_, str):
65+
command_ = func.__name__
66+
logging.debug(f'add command handler. command:{command_} => {func}')
6567
try:
66-
self.dispatcher.add_handler(CommandHandler(command,func,*args,**kwargs), group)
68+
self.dispatcher.add_handler(CommandHandler(command_,func,*args,**kwargs), group)
6769
except:
6870
logging.exception('exception while trying to add a command')
6971
BugReporter.exception('exception while trying to add a command')
@@ -76,7 +78,7 @@ def decorator_command(func):
7678
else:
7779
return decorator_command
7880

79-
def messageHandler(self, _func=None, filters=Filters.all, group=1, *args, **kwargs):
81+
def messageHandler(self, filters=Filters.all, group=1, *args, **kwargs):
8082
def decorator_message(func):
8183
logging.debug(f'add message handler. handler: {func}')
8284
try:
@@ -86,11 +88,7 @@ def decorator_message(func):
8688
BugReporter.exception('exception while trying to add a command')
8789

8890
return func
89-
90-
if _func:
91-
return decorator_message(_func)
92-
else:
93-
return decorator_message
91+
return decorator_message
9492

9593
def addHandler(self, handler_:Handler = None, group=1):
9694
def decorator_handler(handler:Handler):
@@ -135,4 +133,8 @@ def fallback(self, handler:Handler):
135133
return handler
136134

137135
def get_handler(self):
138-
return ConversationHandler(self.entry_points, self.state, self.fallbacks, **self.__kwargs)
136+
return ConversationHandler(
137+
entry_points= self.entry_points,
138+
states= self.states,
139+
fallbacks= self.fallbacks,
140+
**self.__kwargs)

main.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,10 @@ def __init__(
9696
data_db,
9797
strings: dict,
9898
bug_reporter = False,
99-
debug = False):
100-
#----[USE SOCKES]----
101-
#import socks
102-
#s = socks.socksocket()
103-
#s.set_proxy(socks.SOCKS5, "localhost", 9090)
104-
#self.updater = Updater(Token, request_kwargs = {'proxy_url': 'socks5h://127.0.0.1:9090/'})
105-
#-----[NO PROXY]-----
106-
self.updater = Updater(Token)
107-
#--------------------
99+
debug = False,
100+
request_kwargs=None):
101+
102+
self.updater = Updater(Token, request_kwargs=request_kwargs)
108103
self.bot = self.updater.bot
109104
self.dispatcher = self.updater.dispatcher
110105
self.token = Token
@@ -448,8 +443,20 @@ def idle(self):
448443

449444
debug = main_config.getboolean('debug',fallback=False)
450445

446+
use_proxy = main_config.getboolean('use-proxy', fallback=False)
447+
proxy_info = None
448+
if use_proxy:
449+
proxy_info={
450+
'proxy_url': main_config.get('proxy-url','socks5h://127.0.0.1:9090')
451+
}
452+
if 'proxy-user' in main_config:
453+
proxy_info['urllib3_proxy_kwargs'] = {
454+
'username': main_config.get('proxy-user'),
455+
'password': main_config.get('proxy-pass')
456+
}
457+
451458
bot_handler = BotHandler(token, main_config.get('source','https://pcworms.blog.ir/rss/'), env,
452-
chats_db, data_db, strings, not bug_reporter_mode == 'off', debug)
459+
chats_db, data_db, strings, not bug_reporter_mode == 'off', debug, proxy_info)
453460
bot_handler.run()
454461
bot_handler.idle()
455462
if bug_reporter_mode in ('online', 'offline'):

0 commit comments

Comments
 (0)