Esempio n. 1
0
 def get_file(self, file_path):
     """
     Will download and return file content
     """
     return get_response('https://api.telegram.org/file/bot{}/{}'.format(
         self.token, file_path),
                         raw=True)
Esempio n. 2
0
 def get_page(self, path, *, return_content=True):
     """ Will get and return page """
     url = self.base_url + "getPage"
     return get_response(url,
                         payload={
                             "path": path,
                             "return_content": return_content
                         })
Esempio n. 3
0
 def get_group_member(self, participant_group, participant):
     """ Will find a group member and return if available """
     if isinstance(participant_group, Group):
         participant_group = participant_group.telegram_id
     if isinstance(participant, Participant):
         participant = participant.id
     url = self.base_url + 'getChatMember'
     payload = {'chat_id': participant_group, 'user_id': participant}
     return get_response(url, payload=payload)
Esempio n. 4
0
 def get_file_info(self, file_id):
     """
     Will get file path from given file_id
     """
     url = self.base_url + 'getFile'
     payload = {'file_id': file_id}
     resp = get_response(url, payload=payload)
     logging.info(resp)
     return resp
Esempio n. 5
0
 def get_author_account_info(self,
                             *,
                             fields=[
                                 "short_name", "author_name", "author_url"
                             ]):
     """ Will get and return author account information - can manually set field 
     ["short_name", "author_name", "author_url", "auth_url", "page_count"] """
     return get_response(self.base_url + "getAccountInfo",
                         payload=self.base_params)
Esempio n. 6
0
 def update_information(self):
     """ Bot will update it's information with getMe """
     url = self.base_url + 'getMe'
     resp = get_response(url)
     if resp:
         self.username = resp.get('username')
         self.first_name = resp.get('first_name')
         self.last_name = resp.get('last_name')
         self.save()
Esempio n. 7
0
 def create_account(short_name, author_name, author_url):
     """ Will create and return telegraph account """
     return get_response(
         DynamicTelegraphPageCreator.base_url + "createAccount",
         payload={
             "short_name": short_name,
             "author_name": author_name,
             "author_url": author_url,
         },
     )
Esempio n. 8
0
 def load_and_set_page(self, path, *, return_content=True):
     """ Will load and use page with given path """
     return self.set_page(
         get_response(
             self.base_url + "getPage",
             payload={
                 "path": path,
                 "return_content": return_content
             },
         ))
Esempio n. 9
0
 def delete_message(self, participant_group: str or Group, message_id: int
                    or str):
     """ Will delete message from the group """
     if not (isinstance(participant_group, str)
             or isinstance(participant_group, int)):
         participant_group = participant_group.telegram_id
     url = self.base_url + 'deleteMessage'
     payload = {'chat_id': participant_group, 'message_id': message_id}
     resp = get_response(url, payload=payload)
     logging.info(resp)
     return resp
Esempio n. 10
0
 def get_chat_participants_count(self, chat: str or int or Group):
     """Will return participants count of given chat - calling getChatMembersCount"""
     if not chat:
         return None
     if isinstance(chat, Group): chat = chat.telegram_id
     url = self.base_url + 'getChatMembersCount'
     payload = {
         'chat_id': chat,
     }
     resp = get_response(url, payload=payload)
     logging.info(f"Got participants count [{resp}] for chat {chat}")
     return resp
Esempio n. 11
0
 def create_page(self, title, content=[], *, return_content=True):
     """ Will create telegraph page """
     return get_response(
         self.base_url + "createPage",
         payload=dict(
             self.base_params, **{
                 "title": title,
                 "author_name": self.author_name,
                 "author_url": self.author_url,
                 "content": json.dumps(content),
                 "return_content": return_content,
             }),
     )
Esempio n. 12
0
 def get_updates(self, *, update_last_updated=True, timeout=60):
     """ Will return bot updates """
     url = self.bot.base_url + "getUpdates"  # The URL of getting bot updates
     updates = get_response(
         url,
         payload={
             'offset': self.bot.offset or "",  # Setting offset
             'timeout':
             timeout  # Setting timeout to delay empty updates handling
         })
     if update_last_updated:
         self.bot.last_updated = timezone.now()
         self.bot.save()
     return updates
Esempio n. 13
0
 def forward_message(self, from_group: Group or str or int, to_group: Group
                     or str or int, message_id: int or str):
     """
     Will forward message from one group to another one using message_id
     """
     if isinstance(from_group, Group): from_group = from_group.telegram_id
     if isinstance(to_group, Group): to_group = to_group.telegram_id
     url = self.base_url + 'forwardMessage'
     payload = {
         'from_chat_id': from_group,
         'chat_id': to_group,
         'message_id': message_id
     }
     resp = get_response(url, payload=payload)
     logging.info(resp)
     return resp
Esempio n. 14
0
 def send_document(self,
                   participant_group: 'text/id or group',
                   document: io.BufferedReader,
                   *,
                   caption='',
                   reply_to_message_id=None):
     """ Will send a document to the group """
     if not (isinstance(participant_group, str)
             or isinstance(participant_group, int)):
         participant_group = participant_group.telegram_id
     url = self.base_url + 'sendDocument'
     payload = {
         'chat_id': participant_group,
         'caption': caption,
         'reply_to_message_id': reply_to_message_id,
     }
     files = {'document': document}
     resp = get_response(url, payload=payload, files=files)
     logging.info(resp)
     return resp
Esempio n. 15
0
    def send_message(self,
                     group: 'text/id or group',
                     text,
                     *,
                     parse_mode='HTML',
                     reply_to_message_id=None):
        """ Will send a message to the group """
        if not (isinstance(group, str) or isinstance(group, int)):
            group = group.telegram_id

        text = str(text)

        if parse_mode == 'Markdown':
            text = text.replace('_', '\_')
        blocks = []
        if len(text) > MESSAGE_MAX_LENGTH:
            current = text
            while len(current) > MESSAGE_MAX_LENGTH:
                f = current.rfind('. ', 0, MESSAGE_MAX_LENGTH)
                blocks.append(current[:f + 1])
                current = current[f + 2:]
            blocks.append(current)
        else:
            blocks.append(text)
        resp = []
        for message in blocks:
            url = self.base_url + 'sendMessage'
            payload = {
                'chat_id':
                group,
                'text':
                message.replace('<', '&lt;').replace('\\&lt;', '<'),
                'reply_to_message_id':
                reply_to_message_id if not resp else resp[-1].get('message_id')
            }
            if parse_mode:
                payload['parse_mode'] = parse_mode
            resp_c = get_response(url, payload=payload)
            logging.info(resp_c)
            resp.append(resp_c)
        return resp
Esempio n. 16
0
 def author_account_info(self):
     """ Will get and return author account information """
     return get_response(self.base_url + "getAccountInfo",
                         payload=self.base_params)
Esempio n. 17
0
 def revoke_access_token(self):
     """ Will revoke access token and return new account information """
     account = get_response(self.base_url + "revokeAccessToken",
                            payload=self.base_params)
     self.access_token = account.get("access_token") or self.access_token
     return account