Skip to content
This repository has been archived by the owner on Nov 11, 2022. It is now read-only.

Commit

Permalink
Add support for thumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
alissonlauffer committed Nov 14, 2019
1 parent a9a701f commit 5c5b695
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
55 changes: 30 additions & 25 deletions amanobot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,25 @@ def split_media(input_media, name_generator):
legal_media, attachments = map(list, zip(*splitted))
files_to_attach = dict([a for a in attachments if a is not None])

return (legal_media, files_to_attach)
return legal_media, files_to_attach


PY_3 = sys.version_info.major >= 3
_string_type = str if PY_3 else basestring
_file_type = io.IOBase if PY_3 else file


def _isstring(s):
return isinstance(s, _string_type)


def _isfile(f):
return isinstance(f, _file_type)


from . import helper


def flavor_router(routing_table):
router = helper.Router(flavor, routing_table)
return router.route
Expand All @@ -324,6 +327,7 @@ def __init__(self, token):
def _strip(params, more=[]):
return {key: value for key,value in params.items() if key not in ['self']+more}


def _rectify(params):
def make_jsonable(value):
if isinstance(value, list):
Expand Down Expand Up @@ -495,13 +499,14 @@ def handle(self, msg):
def _api_request(self, method, params=None, files=None, **kwargs):
return api.request((self._token, method, params, files), **kwargs)

def _api_request_with_file(self, method, params, file_key, file_value, **kwargs):
if _isstring(file_value):
params[file_key] = file_value
return self._api_request(method, _rectify(params), **kwargs)
else:
files = {file_key: file_value}
return self._api_request(method, _rectify(params), files, **kwargs)
def _api_request_with_file(self, method, params, files, **kwargs):
params.update({
k: v for k, v in files.items() if _isstring(v)})

files = {
k: v for k, v in files.items() if v is not None and not _isstring(v)}

return self._api_request(method, _rectify(params), files, **kwargs)

def getMe(self):
""" See: https://core.telegram.org/bots/api#getme """
Expand Down Expand Up @@ -541,7 +546,7 @@ def sendPhoto(self, chat_id, photo,
filename is a unicode string.
"""
p = _strip(locals(), more=['photo'])
return self._api_request_with_file('sendPhoto', _rectify(p), 'photo', photo)
return self._api_request_with_file('sendPhoto', _rectify(p), {'photo': photo})

def sendAudio(self, chat_id, audio,
caption=None,
Expand All @@ -558,8 +563,8 @@ def sendAudio(self, chat_id, audio,
:param audio: Same as ``photo`` in :meth:`amanobot.Bot.sendPhoto`
"""
p = _strip(locals(), more=['audio'])
return self._api_request_with_file('sendAudio', _rectify(p), 'audio', audio)
p = _strip(locals(), more=['audio', 'thumb'])
return self._api_request_with_file('sendAudio', _rectify(p), {'audio': audio, 'thumb': thumb})

def sendDocument(self, chat_id, document,
thumb=None,
Expand All @@ -573,8 +578,8 @@ def sendDocument(self, chat_id, document,
:param document: Same as ``photo`` in :meth:`amanobot.Bot.sendPhoto`
"""
p = _strip(locals(), more=['document'])
return self._api_request_with_file('sendDocument', _rectify(p), 'document', document)
p = _strip(locals(), more=['document', 'thumb'])
return self._api_request_with_file('sendDocument', _rectify(p), {'document': document, 'thumb': thumb})

def sendVideo(self, chat_id, video,
duration=None,
Expand All @@ -592,8 +597,8 @@ def sendVideo(self, chat_id, video,
:param video: Same as ``photo`` in :meth:`amanobot.Bot.sendPhoto`
"""
p = _strip(locals(), more=['video'])
return self._api_request_with_file('sendVideo', _rectify(p), 'video', video)
p = _strip(locals(), more=['video', 'thumb'])
return self._api_request_with_file('sendVideo', _rectify(p), {'video': video, 'thumb': thumb})

def sendAnimation(self, chat_id, animation,
duration=None,
Expand All @@ -610,8 +615,8 @@ def sendAnimation(self, chat_id, animation,
:param animation: Same as ``photo`` in :meth:`amanobot.Bot.sendPhoto`
"""
p = _strip(locals(), more=['animation'])
return self._api_request_with_file('sendAnimation', _rectify(p), 'animation', animation)
p = _strip(locals(), more=['animation', 'thumb'])
return self._api_request_with_file('sendAnimation', _rectify(p), {'animation': animation, 'thumb': thumb})

def sendVoice(self, chat_id, voice,
caption=None,
Expand All @@ -626,7 +631,7 @@ def sendVoice(self, chat_id, voice,
:param voice: Same as ``photo`` in :meth:`amanobot.Bot.sendPhoto`
"""
p = _strip(locals(), more=['voice'])
return self._api_request_with_file('sendVoice', _rectify(p), 'voice', voice)
return self._api_request_with_file('sendVoice', _rectify(p), {'voice': voice})

def sendVideoNote(self, chat_id, video_note,
duration=None,
Expand All @@ -645,8 +650,8 @@ def sendVideoNote(self, chat_id, video_note,
it being specified. Supply any integer you want. It seems to have no effect
on the video note's display size.
"""
p = _strip(locals(), more=['video_note'])
return self._api_request_with_file('sendVideoNote', _rectify(p), 'video_note', video_note)
p = _strip(locals(), more=['video_note', 'thumb'])
return self._api_request_with_file('sendVideoNote', _rectify(p), {'video_note': video_note, 'thumb': thumb})

def sendMediaGroup(self, chat_id, media,
disable_notification=None,
Expand Down Expand Up @@ -858,7 +863,7 @@ def exportChatInviteLink(self, chat_id):
def setChatPhoto(self, chat_id, photo):
""" See: https://core.telegram.org/bots/api#setchatphoto """
p = _strip(locals(), more=['photo'])
return self._api_request_with_file('setChatPhoto', _rectify(p), 'photo', photo)
return self._api_request_with_file('setChatPhoto', _rectify(p), {'photo': photo})

def deleteChatPhoto(self, chat_id):
""" See: https://core.telegram.org/bots/api#deletechatphoto """
Expand Down Expand Up @@ -1037,7 +1042,7 @@ def sendSticker(self, chat_id, sticker,
:param sticker: Same as ``photo`` in :meth:`amanobot.Bot.sendPhoto`
"""
p = _strip(locals(), more=['sticker'])
return self._api_request_with_file('sendSticker', _rectify(p), 'sticker', sticker)
return self._api_request_with_file('sendSticker', _rectify(p), {'sticker': sticker})

def getStickerSet(self, name):
"""
Expand All @@ -1051,7 +1056,7 @@ def uploadStickerFile(self, user_id, png_sticker):
See: https://core.telegram.org/bots/api#uploadstickerfile
"""
p = _strip(locals(), more=['png_sticker'])
return self._api_request_with_file('uploadStickerFile', _rectify(p), 'png_sticker', png_sticker)
return self._api_request_with_file('uploadStickerFile', _rectify(p), {'png_sticker': png_sticker})

def createNewStickerSet(self, user_id, name, title, png_sticker, emojis,
contains_masks=None,
Expand All @@ -1060,15 +1065,15 @@ def createNewStickerSet(self, user_id, name, title, png_sticker, emojis,
See: https://core.telegram.org/bots/api#createnewstickerset
"""
p = _strip(locals(), more=['png_sticker'])
return self._api_request_with_file('createNewStickerSet', _rectify(p), 'png_sticker', png_sticker)
return self._api_request_with_file('createNewStickerSet', _rectify(p), {'png_sticker': png_sticker})

def addStickerToSet(self, user_id, name, png_sticker, emojis,
mask_position=None):
"""
See: https://core.telegram.org/bots/api#addstickertoset
"""
p = _strip(locals(), more=['png_sticker'])
return self._api_request_with_file('addStickerToSet', _rectify(p), 'png_sticker', png_sticker)
return self._api_request_with_file('addStickerToSet', _rectify(p), {'png_sticker': png_sticker})

def setStickerPositionInSet(self, sticker, position):
"""
Expand Down
52 changes: 27 additions & 25 deletions amanobot/aio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ async def handle(self, msg):
async def _api_request(self, method, params=None, files=None, **kwargs):
return await api.request((self._token, method, params, files), **kwargs)

async def _api_request_with_file(self, method, params, file_key, file_value, **kwargs):
if _isstring(file_value):
params[file_key] = file_value
return await self._api_request(method, _rectify(params), **kwargs)
else:
files = {file_key: file_value}
return await self._api_request(method, _rectify(params), files, **kwargs)
def _api_request_with_file(self, method, params, files, **kwargs):
params.update({
k: v for k, v in files.items() if _isstring(v)})

files = {
k: v for k, v in files.items() if v is not None and not _isstring(v)}

return await self._api_request(method, _rectify(params), files, **kwargs)

async def getMe(self):
""" See: https://core.telegram.org/bots/api#getme """
Expand Down Expand Up @@ -123,7 +124,7 @@ async def sendPhoto(self, chat_id, photo,
filename is a unicode string.
"""
p = _strip(locals(), more=['photo'])
return await self._api_request_with_file('sendPhoto', _rectify(p), 'photo', photo)
return await self._api_request_with_file('sendPhoto', _rectify(p), {'photo': photo})

async def sendAudio(self, chat_id, audio,
caption=None,
Expand All @@ -140,8 +141,8 @@ async def sendAudio(self, chat_id, audio,
:param audio: Same as ``photo`` in :meth:`amanobot.aio.Bot.sendPhoto`
"""
p = _strip(locals(), more=['audio'])
return await self._api_request_with_file('sendAudio', _rectify(p), 'audio', audio)
p = _strip(locals(), more=['audio', 'thumb'])
return await self._api_request_with_file('sendAudio', _rectify(p), {'audio': audio, 'thumb': thumb})

async def sendDocument(self, chat_id, document,
thumb=None,
Expand All @@ -155,8 +156,8 @@ async def sendDocument(self, chat_id, document,
:param document: Same as ``photo`` in :meth:`amanobot.aio.Bot.sendPhoto`
"""
p = _strip(locals(), more=['document'])
return await self._api_request_with_file('sendDocument', _rectify(p), 'document', document)
p = _strip(locals(), more=['document', 'thumb'])
return await self._api_request_with_file('sendDocument', _rectify(p), {'document': document, 'thumb': thumb})

async def sendVideo(self, chat_id, video,
duration=None,
Expand All @@ -174,8 +175,8 @@ async def sendVideo(self, chat_id, video,
:param video: Same as ``photo`` in :meth:`amanobot.aio.Bot.sendPhoto`
"""
p = _strip(locals(), more=['video'])
return await self._api_request_with_file('sendVideo', _rectify(p), 'video', video)
p = _strip(locals(), more=['video', 'thumb'])
return await self._api_request_with_file('sendVideo', _rectify(p), {'video': video, 'thumb': thumb})

async def sendAnimation(self, chat_id, animation,
duration=None,
Expand All @@ -192,8 +193,8 @@ async def sendAnimation(self, chat_id, animation,
:param animation: Same as ``photo`` in :meth:`amanobot.aio.Bot.sendPhoto`
"""
p = _strip(locals(), more=['animation'])
return await self._api_request_with_file('sendAnimation', _rectify(p), 'animation', animation)
p = _strip(locals(), more=['animation', 'thumb'])
return await self._api_request_with_file('sendAnimation', _rectify(p), {'animation': animation, 'thumb': thumb})

async def sendVoice(self, chat_id, voice,
caption=None,
Expand All @@ -208,26 +209,27 @@ async def sendVoice(self, chat_id, voice,
:param voice: Same as ``photo`` in :meth:`amanobot.aio.Bot.sendPhoto`
"""
p = _strip(locals(), more=['voice'])
return await self._api_request_with_file('sendVoice', _rectify(p), 'voice', voice)
return await self._api_request_with_file('sendVoice', _rectify(p), {'voice': voice})

async def sendVideoNote(self, chat_id, video_note,
duration=None,
length=None,
thumb=None,
disable_notification=None,
reply_to_message_id=None,
reply_markup=None):
"""
See: https://core.telegram.org/bots/api#sendvideonote
:param voice: Same as ``photo`` in :meth:`amanobot.aio.Bot.sendPhoto`
:param video_note: Same as ``photo`` in :meth:`amanobot.aio.Bot.sendPhoto`
:param length:
Although marked as optional, this method does not seem to work without
it being specified. Supply any integer you want. It seems to have no effect
on the video note's display size.
"""
p = _strip(locals(), more=['video_note'])
return await self._api_request_with_file('sendVideoNote', _rectify(p), 'video_note', video_note)
p = _strip(locals(), more=['video_note', 'thumb'])
return await self._api_request_with_file('sendVideoNote', _rectify(p), {'video_note': video_note, 'thumb': thumb})

async def sendMediaGroup(self, chat_id, media,
disable_notification=None,
Expand Down Expand Up @@ -439,7 +441,7 @@ async def exportChatInviteLink(self, chat_id):
async def setChatPhoto(self, chat_id, photo):
""" See: https://core.telegram.org/bots/api#setchatphoto """
p = _strip(locals(), more=['photo'])
return await self._api_request_with_file('setChatPhoto', _rectify(p), 'photo', photo)
return await self._api_request_with_file('setChatPhoto', _rectify(p), {'photo': photo})

async def deleteChatPhoto(self, chat_id):
""" See: https://core.telegram.org/bots/api#deletechatphoto """
Expand Down Expand Up @@ -619,7 +621,7 @@ async def sendSticker(self, chat_id, sticker,
:param sticker: Same as ``photo`` in :meth:`amanobot.aio.Bot.sendPhoto`
"""
p = _strip(locals(), more=['sticker'])
return await self._api_request_with_file('sendSticker', _rectify(p), 'sticker', sticker)
return await self._api_request_with_file('sendSticker', _rectify(p), {'sticker': sticker})

async def getStickerSet(self, name):
"""
Expand All @@ -633,7 +635,7 @@ async def uploadStickerFile(self, user_id, png_sticker):
See: https://core.telegram.org/bots/api#uploadstickerfile
"""
p = _strip(locals(), more=['png_sticker'])
return await self._api_request_with_file('uploadStickerFile', _rectify(p), 'png_sticker', png_sticker)
return await self._api_request_with_file('uploadStickerFile', _rectify(p), {'png_sticker': png_sticker})

async def createNewStickerSet(self, user_id, name, title, png_sticker, emojis,
contains_masks=None,
Expand All @@ -642,15 +644,15 @@ async def createNewStickerSet(self, user_id, name, title, png_sticker, emojis,
See: https://core.telegram.org/bots/api#createnewstickerset
"""
p = _strip(locals(), more=['png_sticker'])
return await self._api_request_with_file('createNewStickerSet', _rectify(p), 'png_sticker', png_sticker)
return await self._api_request_with_file('createNewStickerSet', _rectify(p), {'png_sticker': png_sticker})

async def addStickerToSet(self, user_id, name, png_sticker, emojis,
mask_position=None):
"""
See: https://core.telegram.org/bots/api#addstickertoset
"""
p = _strip(locals(), more=['png_sticker'])
return await self._api_request_with_file('addStickerToSet', _rectify(p), 'png_sticker', png_sticker)
return await self._api_request_with_file('addStickerToSet', _rectify(p), {'png_sticker': png_sticker})

async def setStickerPositionInSet(self, sticker, position):
"""
Expand Down

0 comments on commit 5c5b695

Please sign in to comment.