2015-08-31 12:46:18 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-01-15 16:08:50 +03:00
|
|
|
import random
|
2018-08-17 12:54:26 +03:00
|
|
|
import re
|
2018-01-15 16:08:50 +03:00
|
|
|
import string
|
2015-08-31 12:46:18 +03:00
|
|
|
import threading
|
2017-06-20 15:45:18 +03:00
|
|
|
import traceback
|
2020-05-02 13:09:52 +03:00
|
|
|
import warnings
|
|
|
|
import functools
|
2018-08-17 12:54:26 +03:00
|
|
|
|
2020-08-24 16:02:35 +03:00
|
|
|
import queue as Queue
|
2018-03-10 09:41:34 +03:00
|
|
|
import logging
|
2015-08-31 12:46:18 +03:00
|
|
|
|
2020-07-31 08:39:04 +03:00
|
|
|
try:
|
2020-08-29 21:57:41 +03:00
|
|
|
from PIL import Image
|
2020-07-31 08:39:04 +03:00
|
|
|
from io import BytesIO
|
|
|
|
pil_imported = True
|
|
|
|
except:
|
|
|
|
pil_imported = False
|
|
|
|
|
2018-03-10 09:41:34 +03:00
|
|
|
logger = logging.getLogger('TeleBot')
|
2015-08-31 12:46:18 +03:00
|
|
|
|
2017-07-19 01:35:19 +03:00
|
|
|
thread_local = threading.local()
|
|
|
|
|
2020-11-07 12:52:51 +03:00
|
|
|
content_type_media = [
|
|
|
|
'text', 'audio', 'document', 'photo', 'sticker', 'video', 'video_note', 'voice', 'contact', 'dice', 'poll',
|
|
|
|
'venue', 'location'
|
|
|
|
]
|
|
|
|
|
|
|
|
content_type_service = [
|
|
|
|
'new_chat_members', 'left_chat_member', 'new_chat_title', 'new_chat_photo', 'delete_chat_photo', 'group_chat_created',
|
|
|
|
'supergroup_chat_created', 'channel_chat_created', 'migrate_to_chat_id', 'migrate_from_chat_id', 'pinned_message'
|
|
|
|
]
|
2017-07-19 01:35:19 +03:00
|
|
|
|
2015-10-01 23:03:54 +03:00
|
|
|
class WorkerThread(threading.Thread):
|
2020-07-04 20:45:48 +03:00
|
|
|
count = 0
|
|
|
|
|
|
|
|
def __init__(self, exception_callback=None, queue=None, name=None):
|
|
|
|
if not name:
|
|
|
|
name = "WorkerThread{0}".format(self.__class__.count + 1)
|
|
|
|
self.__class__.count += 1
|
|
|
|
if not queue:
|
|
|
|
queue = Queue.Queue()
|
|
|
|
|
|
|
|
threading.Thread.__init__(self, name=name)
|
|
|
|
self.queue = queue
|
|
|
|
self.daemon = True
|
|
|
|
|
|
|
|
self.received_task_event = threading.Event()
|
|
|
|
self.done_event = threading.Event()
|
|
|
|
self.exception_event = threading.Event()
|
|
|
|
self.continue_event = threading.Event()
|
|
|
|
|
|
|
|
self.exception_callback = exception_callback
|
2020-08-24 16:02:35 +03:00
|
|
|
self.exception_info = None
|
2020-07-04 20:45:48 +03:00
|
|
|
self._running = True
|
|
|
|
self.start()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while self._running:
|
|
|
|
try:
|
|
|
|
task, args, kwargs = self.queue.get(block=True, timeout=.5)
|
|
|
|
self.continue_event.clear()
|
|
|
|
self.received_task_event.clear()
|
|
|
|
self.done_event.clear()
|
|
|
|
self.exception_event.clear()
|
|
|
|
logger.debug("Received task")
|
|
|
|
self.received_task_event.set()
|
|
|
|
|
|
|
|
task(*args, **kwargs)
|
|
|
|
logger.debug("Task complete")
|
|
|
|
self.done_event.set()
|
|
|
|
except Queue.Empty:
|
|
|
|
pass
|
|
|
|
except Exception as e:
|
2020-11-07 14:43:17 +03:00
|
|
|
logger.debug(type(e).__name__ + " occurred, args=" + str(e.args) + "\n" + traceback.format_exc())
|
2020-08-24 16:02:35 +03:00
|
|
|
self.exception_info = e
|
2020-07-04 20:45:48 +03:00
|
|
|
self.exception_event.set()
|
|
|
|
if self.exception_callback:
|
2020-08-24 16:02:35 +03:00
|
|
|
self.exception_callback(self, self.exception_info)
|
2020-07-04 20:45:48 +03:00
|
|
|
self.continue_event.wait()
|
|
|
|
|
|
|
|
def put(self, task, *args, **kwargs):
|
|
|
|
self.queue.put((task, args, kwargs))
|
|
|
|
|
|
|
|
def raise_exceptions(self):
|
|
|
|
if self.exception_event.is_set():
|
2020-08-24 16:02:35 +03:00
|
|
|
raise self.exception_info
|
2020-07-04 20:45:48 +03:00
|
|
|
|
|
|
|
def clear_exceptions(self):
|
|
|
|
self.exception_event.clear()
|
|
|
|
self.continue_event.set()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self._running = False
|
2015-08-31 12:46:18 +03:00
|
|
|
|
|
|
|
|
2015-10-01 23:03:54 +03:00
|
|
|
class ThreadPool:
|
|
|
|
|
|
|
|
def __init__(self, num_threads=2):
|
|
|
|
self.tasks = Queue.Queue()
|
|
|
|
self.workers = [WorkerThread(self.on_exception, self.tasks) for _ in range(num_threads)]
|
2015-08-31 12:46:18 +03:00
|
|
|
self.num_threads = num_threads
|
|
|
|
|
2015-10-01 23:03:54 +03:00
|
|
|
self.exception_event = threading.Event()
|
2020-08-24 16:02:35 +03:00
|
|
|
self.exception_info = None
|
2015-10-01 23:03:54 +03:00
|
|
|
|
2015-08-31 12:46:18 +03:00
|
|
|
def put(self, func, *args, **kwargs):
|
|
|
|
self.tasks.put((func, args, kwargs))
|
|
|
|
|
2015-10-01 23:03:54 +03:00
|
|
|
def on_exception(self, worker_thread, exc_info):
|
2020-08-24 16:02:35 +03:00
|
|
|
self.exception_info = exc_info
|
2015-10-01 23:03:54 +03:00
|
|
|
self.exception_event.set()
|
|
|
|
worker_thread.continue_event.set()
|
|
|
|
|
|
|
|
def raise_exceptions(self):
|
|
|
|
if self.exception_event.is_set():
|
2020-08-24 16:02:35 +03:00
|
|
|
raise self.exception_info
|
2015-10-01 23:03:54 +03:00
|
|
|
|
|
|
|
def clear_exceptions(self):
|
|
|
|
self.exception_event.clear()
|
|
|
|
|
2015-08-31 12:46:18 +03:00
|
|
|
def close(self):
|
|
|
|
for worker in self.workers:
|
|
|
|
worker.stop()
|
|
|
|
for worker in self.workers:
|
|
|
|
worker.join()
|
|
|
|
|
2015-10-01 12:33:23 +03:00
|
|
|
|
2015-08-31 12:46:18 +03:00
|
|
|
class AsyncTask:
|
|
|
|
def __init__(self, target, *args, **kwargs):
|
|
|
|
self.target = target
|
|
|
|
self.args = args
|
|
|
|
self.kwargs = kwargs
|
|
|
|
|
|
|
|
self.done = False
|
|
|
|
self.thread = threading.Thread(target=self._run)
|
|
|
|
self.thread.start()
|
|
|
|
|
|
|
|
def _run(self):
|
|
|
|
try:
|
|
|
|
self.result = self.target(*self.args, **self.kwargs)
|
2020-08-24 16:02:35 +03:00
|
|
|
except Exception as e:
|
|
|
|
self.result = e
|
2015-08-31 12:46:18 +03:00
|
|
|
self.done = True
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
if not self.done:
|
|
|
|
self.thread.join()
|
2015-10-01 23:03:54 +03:00
|
|
|
if isinstance(self.result, BaseException):
|
2020-08-24 16:02:35 +03:00
|
|
|
raise self.result
|
2015-08-31 12:46:18 +03:00
|
|
|
else:
|
|
|
|
return self.result
|
|
|
|
|
|
|
|
|
2018-07-02 18:13:11 +03:00
|
|
|
def async_dec():
|
2015-08-31 12:46:18 +03:00
|
|
|
def decorator(fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
return AsyncTask(fn, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def is_string(var):
|
2020-08-24 16:02:35 +03:00
|
|
|
return isinstance(var, str)
|
2015-08-31 12:46:18 +03:00
|
|
|
|
2020-07-31 23:30:38 +03:00
|
|
|
def is_dict(var):
|
|
|
|
return isinstance(var, dict)
|
|
|
|
|
|
|
|
def is_bytes(var):
|
|
|
|
return isinstance(var, bytes)
|
|
|
|
|
2020-07-31 08:39:04 +03:00
|
|
|
def is_pil_image(var):
|
2020-08-29 21:57:41 +03:00
|
|
|
return pil_imported and isinstance(var, Image.Image)
|
2020-07-31 08:39:04 +03:00
|
|
|
|
|
|
|
def pil_image_to_file(image, extension='JPEG', quality='web_low'):
|
|
|
|
if pil_imported:
|
|
|
|
photoBuffer = BytesIO()
|
|
|
|
image.convert('RGB').save(photoBuffer, extension, quality=quality)
|
|
|
|
photoBuffer.seek(0)
|
|
|
|
|
|
|
|
return photoBuffer
|
|
|
|
else:
|
|
|
|
raise RuntimeError('PIL module is not imported')
|
|
|
|
|
2015-08-31 12:46:18 +03:00
|
|
|
def is_command(text):
|
|
|
|
"""
|
|
|
|
Checks if `text` is a command. Telegram chat commands start with the '/' character.
|
|
|
|
:param text: Text to check.
|
|
|
|
:return: True if `text` is a command, else False.
|
|
|
|
"""
|
2020-08-19 23:57:48 +03:00
|
|
|
if (text is None): return None
|
2015-08-31 12:46:18 +03:00
|
|
|
return text.startswith('/')
|
|
|
|
|
|
|
|
|
|
|
|
def extract_command(text):
|
|
|
|
"""
|
|
|
|
Extracts the command from `text` (minus the '/') if `text` is a command (see is_command).
|
|
|
|
If `text` is not a command, this function returns None.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
extract_command('/help'): 'help'
|
|
|
|
extract_command('/help@BotName'): 'help'
|
|
|
|
extract_command('/search black eyed peas'): 'search'
|
|
|
|
extract_command('Good day to you'): None
|
|
|
|
|
|
|
|
:param text: String to extract the command from
|
|
|
|
:return: the command if `text` is a command (according to is_command), else None.
|
|
|
|
"""
|
2020-08-19 23:57:48 +03:00
|
|
|
if (text is None): return None
|
2015-08-31 12:46:18 +03:00
|
|
|
return text.split()[0].split('@')[0][1:] if is_command(text) else None
|
|
|
|
|
|
|
|
|
|
|
|
def split_string(text, chars_per_string):
|
|
|
|
"""
|
|
|
|
Splits one string into multiple strings, with a maximum amount of `chars_per_string` characters per string.
|
|
|
|
This is very useful for splitting one giant message into multiples.
|
|
|
|
|
|
|
|
:param text: The text to split
|
|
|
|
:param chars_per_string: The number of characters per line the text is split into.
|
|
|
|
:return: The splitted text as a list of strings.
|
|
|
|
"""
|
|
|
|
return [text[i:i + chars_per_string] for i in range(0, len(text), chars_per_string)]
|
2015-10-01 23:03:54 +03:00
|
|
|
|
|
|
|
# CREDITS TO http://stackoverflow.com/questions/12317940#answer-12320352
|
|
|
|
def or_set(self):
|
|
|
|
self._set()
|
|
|
|
self.changed()
|
|
|
|
|
|
|
|
|
|
|
|
def or_clear(self):
|
|
|
|
self._clear()
|
|
|
|
self.changed()
|
|
|
|
|
|
|
|
|
|
|
|
def orify(e, changed_callback):
|
2021-01-14 03:44:37 +03:00
|
|
|
if not hasattr(e, "_set"):
|
|
|
|
e._set = e.set
|
|
|
|
if not hasattr(e, "_clear"):
|
|
|
|
e._clear = e.clear
|
2015-10-01 23:03:54 +03:00
|
|
|
e.changed = changed_callback
|
|
|
|
e.set = lambda: or_set(e)
|
|
|
|
e.clear = lambda: or_clear(e)
|
|
|
|
|
|
|
|
def OrEvent(*events):
|
|
|
|
or_event = threading.Event()
|
|
|
|
def changed():
|
2021-01-14 03:44:37 +03:00
|
|
|
bools = [ev.is_set() for ev in events]
|
2015-10-01 23:03:54 +03:00
|
|
|
if any(bools):
|
|
|
|
or_event.set()
|
|
|
|
else:
|
|
|
|
or_event.clear()
|
2015-10-03 13:48:56 +03:00
|
|
|
|
|
|
|
def busy_wait():
|
|
|
|
while not or_event.is_set():
|
|
|
|
or_event._wait(3)
|
|
|
|
|
2015-10-01 23:03:54 +03:00
|
|
|
for e in events:
|
|
|
|
orify(e, changed)
|
2015-10-03 13:48:56 +03:00
|
|
|
or_event._wait = or_event.wait
|
|
|
|
or_event.wait = busy_wait
|
2015-10-01 23:03:54 +03:00
|
|
|
changed()
|
|
|
|
return or_event
|
2016-03-17 08:18:08 +03:00
|
|
|
|
|
|
|
def extract_arguments(text):
|
|
|
|
"""
|
|
|
|
Returns the argument after the command.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
extract_arguments("/get name"): 'name'
|
|
|
|
extract_arguments("/get"): ''
|
|
|
|
extract_arguments("/get@botName name"): 'name'
|
|
|
|
|
|
|
|
:param text: String to extract the arguments from a command
|
|
|
|
:return: the arguments if `text` is a command (according to is_command), else None.
|
|
|
|
"""
|
2020-05-01 11:25:51 +03:00
|
|
|
regexp = re.compile(r"/\w*(@\w*)*\s*([\s\S]*)",re.IGNORECASE)
|
2016-03-17 08:18:08 +03:00
|
|
|
result = regexp.match(text)
|
2016-03-17 08:21:02 +03:00
|
|
|
return result.group(2) if is_command(text) else None
|
2017-07-19 01:35:19 +03:00
|
|
|
|
|
|
|
|
2018-09-06 12:42:44 +03:00
|
|
|
def per_thread(key, construct_value, reset=False):
|
|
|
|
if reset or not hasattr(thread_local, key):
|
2017-07-19 01:35:19 +03:00
|
|
|
value = construct_value()
|
|
|
|
setattr(thread_local, key, value)
|
2018-09-06 12:42:44 +03:00
|
|
|
|
|
|
|
return getattr(thread_local, key)
|
2018-01-15 16:08:50 +03:00
|
|
|
|
2020-07-31 23:30:38 +03:00
|
|
|
def chunks(lst, n):
|
|
|
|
"""Yield successive n-sized chunks from lst."""
|
|
|
|
# https://stackoverflow.com/a/312464/9935473
|
|
|
|
for i in range(0, len(lst), n):
|
|
|
|
yield lst[i:i + n]
|
2018-01-15 16:08:50 +03:00
|
|
|
|
|
|
|
def generate_random_token():
|
2020-05-02 13:09:52 +03:00
|
|
|
return ''.join(random.sample(string.ascii_letters, 16))
|
|
|
|
|
|
|
|
def deprecated(func):
|
|
|
|
"""This is a decorator which can be used to mark functions
|
|
|
|
as deprecated. It will result in a warning being emitted
|
|
|
|
when the function is used."""
|
|
|
|
# https://stackoverflow.com/a/30253848/441814
|
|
|
|
@functools.wraps(func)
|
|
|
|
def new_func(*args, **kwargs):
|
|
|
|
warnings.simplefilter('always', DeprecationWarning) # turn off filter
|
|
|
|
warnings.warn("Call to deprecated function {}.".format(func.__name__),
|
|
|
|
category=DeprecationWarning,
|
|
|
|
stacklevel=2)
|
|
|
|
warnings.simplefilter('default', DeprecationWarning) # reset filter
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return new_func
|