1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Some Updates

> Added lot of type hints to types.py
> Added some new fields from TelegramBotAPI to pyTelegramBotAPI
> fixed `circular import error in util.py
> Added functions `log_out` and `close` to __init__.py and apihelper.py
> And some more small changes
This commit is contained in:
SwissCorePy
2021-06-17 20:28:53 +02:00
parent fe9df2df8c
commit bbafdd1c1d
5 changed files with 821 additions and 858 deletions

View File

@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
# credits: https://adamj.eu/tech/2021/05/13/python-type-hints-how-to-fix-circular-imports/
from __future__ import annotations
import random
import re
import string
@@ -6,11 +8,13 @@ import threading
import traceback
import warnings
import functools
from typing import Any, List, Dict
from typing import Any, List, Dict, TYPE_CHECKING
import queue as Queue
import logging
# from telebot import types
# credits: https://adamj.eu/tech/2021/05/13/python-type-hints-how-to-fix-circular-imports/
if TYPE_CHECKING:
from telebot import types
try:
from PIL import Image
@@ -289,7 +293,7 @@ def escape(text: str) -> str:
return text
def user_link(user, include_id: bool=False) -> str:
def user_link(user: types.User, include_id: bool=False) -> str:
"""
Returns an HTML user link. This is useful for reports.
Attention: Don't forget to set parse_mode to 'HTML'!
@@ -306,41 +310,40 @@ def user_link(user, include_id: bool=False) -> str:
+ (f" (<pre>{user.id}</pre>)" if include_id else ""))
# def quick_markup(values: Dict[str, Dict[str, Any]], row_width: int=2):
# """
# Returns a reply markup from a dict in this format: {'text': kwargs}
# This is useful to avoid always typing 'btn1 = InlineKeyboardButton(...)' 'btn2 = InlineKeyboardButton(...)'
#
# Example:
# quick_markup({
# 'Twitter': {'url': 'https://twitter.com'},
# 'Facebook': {'url': 'https://facebook.com'},
# 'Back': {'callback_data': 'whatever'}
# }, row_width=2):
# returns an InlineKeyboardMarkup with two buttons in a row, one leading to Twitter, the other to facebook
# and a back button below
#
# kwargs can be:
# {
# 'url': None,
# 'callback_data': None,
# 'switch_inline_query': None,
# 'switch_inline_query_current_chat': None,
# 'callback_game': None,
# 'pay': None,
# 'login_url': None
# }
#
# :param values: a dict containing all buttons to create in this format: {text: kwargs} {str:}
# :param row_width:
# :return: InlineKeyboardMarkup
# """
# markup = types.InlineKeyboardMarkup(row_width=row_width)
# buttons = []
# for text, kwargs in values.items():
# buttons.append(types.InlineKeyboardButton(text=text, **kwargs))
# markup.add(*buttons)
# return markup
def quick_markup(values: Dict[str, Dict[str, Any]], row_width: int=2) -> types.InlineKeyboardMarkup:
"""
Returns a reply markup from a dict in this format: {'text': kwargs}
This is useful to avoid always typing 'btn1 = InlineKeyboardButton(...)' 'btn2 = InlineKeyboardButton(...)'
Example:
quick_markup({
'Twitter': {'url': 'https://twitter.com'},
'Facebook': {'url': 'https://facebook.com'},
'Back': {'callback_data': 'whatever'}
}, row_width=2):
returns an InlineKeyboardMarkup with two buttons in a row, one leading to Twitter, the other to facebook
and a back button below
kwargs can be:
{
'url': None,
'callback_data': None,
'switch_inline_query': None,
'switch_inline_query_current_chat': None,
'callback_game': None,
'pay': None,
'login_url': None
}
:param values: a dict containing all buttons to create in this format: {text: kwargs} {str:}
:return: InlineKeyboardMarkup
"""
markup = types.InlineKeyboardMarkup(row_width=row_width)
buttons = []
for text, kwargs in values.items():
buttons.append(types.InlineKeyboardButton(text=text, **kwargs))
markup.add(*buttons)
return markup
# CREDITS TO http://stackoverflow.com/questions/12317940#answer-12320352