Example #1
0
 def send_meme(self,
               chat_id: int,
               session: requests.Session = requests.Session(),
               meme_keyboard: dict = None,
               extra_text: str = '') -> int:
     tags_string = str()
     for tag in self.tags.all():
         tags_string += f'\n<code>{tag.tag}</code>'
     meme_dict = {
         'caption':
         f'{extra_text}<b>Meme Name</b>: <code>{self.name}</code>\n\n<b>Meme Tags 👇</b>{tags_string}',
         'parse_mode': 'HTML',
         'chat_id': chat_id
     }
     if meme_keyboard:
         meme_dict['reply_markup'] = json.dumps(meme_keyboard)
     if self.type == MemeType.VOICE:
         meme_dict['voice'] = self.file_id
         request_type = 'Voice'
     else:
         meme_dict['video'] = self.file_id
         request_type = 'Video'
     with session.get(
             f'https://api.telegram.org/bot{settings.MEME}/send{request_type}',
             params=meme_dict,
             timeout=settings.REQUESTS_TIMEOUT * 10) as response:
         if response.status_code == 200:
             return response.json()['result']['message_id']
         elif response.status_code != 429:
             return 0
         raise TooManyRequests(response.json()['parameters']['retry_after'])
Example #2
0
def send_message(chat_id: int, text: str, session: RequestsSession = RequestsSession()):
    with session.get(
        f'https://api.telegram.org/bot{settings.MEME}/sendMessage',
        params={'chat_id': chat_id, 'text': text}
    ) as response:
        if response.status_code != 429:
            return
        raise TooManyRequests(response.json()['parameters']['retry_after'])
Example #3
0
 def get_chat(self):
     with self.session.get(
         f'{self._BASE_URL}getChat', params=self._BASE_PARAM,
         timeout=settings.REQUESTS_TIMEOUT
     ) as response:
         if response.status_code == 200:
             return response.json().get('result')
         elif response.status_code != 429:
             return {}
         raise TooManyRequests(response.json()['parameters']['retry_after'])
Example #4
0
 def delete_vote(self, session: requests.Session = requests.Session()):
     celery_app.control.revoke(self.task_id)
     with session.get(
             f'https://api.telegram.org/bot{settings.MEME}/deleteMessage',
             params={
                 'chat_id': settings.MEME_CHANNEL,
                 'message_id': self.message_id
             },
             timeout=settings.REQUESTS_TIMEOUT) as response:
         if response.status_code != 429:
             return
         raise TooManyRequests(response.json()['parameters']['retry_after'])
Example #5
0
 def upload_voice(self):
     now = datetime.now()
     if self.database.started and self.database.last_start and \
             (now - self.database.last_start) <= timedelta(seconds=18000):
         return
     with self.session.get(
             f'{self._BASE_URL}sendChatAction', params={**self._BASE_PARAM, 'action': 'upload_voice'},
             timeout=settings.REQUESTS_TIMEOUT
     ) as response:
         if response.status_code != 429:
             self.database.started = response.status_code == 200
             if self.database.started:
                 self.database.last_start = now
             return
         raise TooManyRequests(response.json()['parameters']['retry_after'])
Example #6
0
 def send_current_meme(self, reply_markup: dict = '', reply_to_message_id: int = ''):
     if reply_markup:
         reply_markup = json.dumps(reply_markup)
     params = {
         'caption': self.database.current_meme.name,
         'reply_markup': reply_markup,
         'reply_to_message_id': reply_to_message_id
     }
     if self.database.current_meme.type == models.MemeType.VOICE:
         meme_method = 'Voice'
         params['voice'] = self.database.current_meme.file_id
     else:
         meme_method = 'Video'
         params['video'] = self.database.current_meme.file_id
     with self.session.get(
         f'{self._BASE_URL}send{meme_method}',
         params={**self._BASE_PARAM, **params},
         timeout=settings.REQUESTS_TIMEOUT
     ) as response:
         if response.status_code != 429:
             return
         raise TooManyRequests(response.json()['parameters']['retry_after'])
Example #7
0
def edit_message_reply_markup(
        chat_id: int,
        new_reply_markup: dict,
        message_id: int = None,
        inline_message_id: int = None,
        session: RequestsSession = RequestsSession()
):
    assert message_id is not None or inline_message_id is not None,\
        'You must at least provide message_id or inline_message_id !'
    params = {'chat_id': chat_id, 'reply_markup': json.dumps(new_reply_markup)}
    if message_id:
        params['message_id'] = message_id
    else:
        params['inline_message_id'] = inline_message_id
    with session.get(
        f'https://api.telegram.org/bot{settings.MEME}/editMessageReplyMarkup',
        timeout=(settings.REQUESTS_TIMEOUT * 20),
        params=params
    ) as response:
        if response.status_code != 429:
            return
        raise TooManyRequests(response.json()['parameters']['retry_after'])
Example #8
0
def answer_inline_query(
        inline_query_id: str,
        results: str,
        next_offset: str,
        switch_pm_text: str,
        switch_pm_parameter: str,
        session: RequestsSession
):
    with session.get(
        f'https://api.telegram.org/bot{settings.MEME}/answerInlineQuery',
        params={
            'results': results,
            'next_offset': next_offset,
            'switch_pm_text': switch_pm_text,
            'switch_pm_parameter': switch_pm_parameter,
            'inline_query_id': inline_query_id,
            'cache_time': 0
        },
        timeout=settings.REQUESTS_TIMEOUT
    ) as response:
        if response.status_code != 429:
            return
        raise TooManyRequests(response.json()['parameters']['retry_after'])
Example #9
0
        base_param = {'message_id': message_id, 'reply_markup': reply_markup, 'protect_content': protect_content}
        with self.session.get(
            f'{self._BASE_URL}copyMessage',
            params={
                'from_chat_id': self.database.chat_id,
                'chat_id': chat_id,
                **base_param
            } if not from_chat_id else {'from_chat_id': from_chat_id, 'chat_id': self.database.chat_id, **base_param},
            timeout=settings.REQUESTS_TIMEOUT
        ) as response:
            if response.status_code == 200:
                if (result := response.json())['ok']:
                    return result['result']['message_id']
            elif response.status_code != 429:
                return False
            raise TooManyRequests(response.json()['parameters']['retry_after'])

    def __save_ad(self, ad: models.Ad):
        ad.seen.add(self.database)
        ad.save()

    def send_ad(self):
        for ad in self.__ads:
            self.copy_message(ad.message_id, from_chat_id=ad.chat_id)
            self.__save_ad(ad)

    @property
    def __search_items(self):
        match self.database.search_items:
            case models.User.SearchItems.VIDEOS:
                return Q(type=models.MemeType.VIDEO), Q(meme__type=models.MemeType.VIDEO)