-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
69 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
# 👨🍳 callbaker | ||
|
||
[![Download Callbaker](https://img.shields.io/pypi/v/callbaker.svg)](https://pypi.python.org/pypi/callbaker) | ||
![CodeQL](https://github.com/torrua/callbaker/workflows/CodeQL/badge.svg?branch=master) | ||
[![Build Status](https://travis-ci.com/torrua/callbaker.svg?branch=main)](https://travis-ci.com/torrua/callbaker) | ||
[![codecov](https://codecov.io/gh/torrua/callbaker/branch/master/graph/badge.svg?token=CHCS5JEGZI)](https://codecov.io/gh/torrua/callbaker) | ||
|
||
Telegram callback queries converter | ||
|
||
## What it is | ||
|
||
This tiny package can help you parse the callback data request response for telegram bots. | ||
|
||
## How to install | ||
|
||
``pip install callbaker`` | ||
|
||
## How to use | ||
|
||
Under developing... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from callbaker import EMS, IMS, DMV | ||
|
||
|
||
def info_from_callback(call_data: str, separators: tuple = (EMS, IMS, DMV)) -> dict: | ||
""" | ||
:param call_data: | ||
:param separators: | ||
:return: | ||
""" | ||
|
||
if not isinstance(call_data, str): | ||
raise TypeError("call_data should be a str type. You input %s." % type(call_data)) | ||
|
||
for sep in separators: | ||
if not isinstance(sep, str): | ||
raise TypeError("Separator should be a str type. You input %s." % type(sep)) | ||
|
||
_ems, _ims, _ = separators | ||
separated_items = call_data.split(_ems) | ||
parsed_items = [element.split(_ims) | ||
for element in separated_items if element] | ||
result = {k: v for k, v in parsed_items if k and v} | ||
|
||
for mark, value in result.items(): | ||
if value.isdigit(): | ||
result[mark] = int(value) | ||
_ = [result.update({mark: item}) for item in (False, True, None) if value == str(item)] | ||
return result | ||
|
||
|
||
def callback_from_info(info: dict, separators: tuple = (EMS, IMS, DMV)) -> str: | ||
""" | ||
:param info: | ||
:param separators: | ||
:return: | ||
""" | ||
if not isinstance(info, dict): | ||
raise TypeError("Info should be a dict type. You input %s." % type(info)) | ||
|
||
for sep in separators: | ||
if not isinstance(sep, str): | ||
raise TypeError("Separator should be a str type. You input %s." % type(sep)) | ||
|
||
_ems, _ims, _ = separators | ||
|
||
callback = "".join(["%s%s%s%s" % (_ems, mark, _ims, value) for mark, value in info.items()]) | ||
|
||
if len(callback) > 64: | ||
raise ValueError("The length of callback_data should not be more that 64 symbols." | ||
"Your callback's length is %s symbols." % len(callback)) | ||
return callback |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters