Esempio n. 1
0
    def send_message(self, chat_id, text, parse_mode="", disable_web_page_preview=False, reply_to_message_id=0,
                     reply_markup=None):
        """
        Use this method to send text messages.

        :param chat_id: Unique identifier for the message recipient
        :type chat_id: int
        :param text: Text of the message to be sent
        :type text: str
        :param parse_mode: Send Markdown, if you want Telegram apps to show bold,
        italic and inline URLs in your bot's message.
        For the moment, only Telegram for Android supports this.
        :type parse_mode: str
        :param disable_web_page_preview: Disables link previews for links in this message
        :type disable_web_page_preview: bool
        :param reply_to_message_id: If the message is a reply, ID of the original message
        :type reply_to_message_id: int
        :param reply_markup: A JSON-serialized object for a custom reply keyboard
        :type reply_markup: ReplyKeyboardMarkup|ReplyKeyboardHide|ForceReply
        :return: On success, the sent Message is returned
        :rtype: Message
        """
        compiled_query = (
            'sendMessage?chat_id=%i&text=%s&parse_mode=%s&disable_web_page_preview=%s&reply_to_message_id=%i' % (
                chat_id, text, parse_mode, disable_web_page_preview, reply_to_message_id))
        if reply_markup is not None:
            compiled_query += '&reply_markup=%s' % reply_markup.to_json()
        data = TbotPy.__urlopen(self.endpoint + compiled_query)
        return Message.build_from_json(json.loads(data)['result'])
Esempio n. 2
0
    def forward_message(self, chat_id, from_chat_id, message_id):
        """
        Use this method to forward messages of any kind.

        :param chat_id: Unique identifier for the message recipient
        :type chat_id: int
        :param from_chat_id: Unique identifier for the chat where the original message was sent
        :type from_chat_id: int
        :param message_id: Unique message identifier
        :type message_id: int
        :return: On success, the sent Message is returned
        :rtype: Message
        """
        compiled_query = 'forwardMessage?chat_id=%i&from_chat_id=%i&message_id=%i' % (chat_id, from_chat_id, message_id)
        data = TbotPy.__urlopen(self.endpoint + compiled_query)
        return Message.build_from_json(json.loads(data)['result'])
Esempio n. 3
0
    def get_updates(self, timeout=5, confirm_update=True):
        """
        Use this method to receive incoming updates using long polling.

        :param timeout: Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling
        :type timeout: int
        :param confirm_update: Confirm last update
        :type confirm_update: bool
        :return: An Array of Update objects is returned
        :rtype: tuple of Message
        """
        messages = []
        query = self.endpoint + "getUpdates"
        data = TbotPy.__urlopen('%s?%s=%i' % (query, 'timeout', timeout))
        results = json.loads(data)['result']
        for result in results:
            messages.append(Message.build_from_json(result['message']))
        if len(results) > 0:
            self.last_update = int(results[len(results) - 1]['update_id']) + 1
            if confirm_update:
                urlopen(query + '?offset=%d' % self.last_update)
        return tuple(messages)
Esempio n. 4
0
    def send_location(self, chat_id, latitude, longitude, reply_to_message_id=0, reply_markup=None):
        """
        Use this method to send point on the map.

        :param chat_id: Unique identifier for the message recipient
        :type chat_id: int
        :param latitude: Latitude of location
        :type latitude: float
        :param longitude: Longitude of location
        :type longitude: float
        :param reply_to_message_id: If the message is a reply, ID of the original message
        :type reply_to_message_id: int
        :param reply_markup: A JSON-serialized object for a custom reply keyboard
        :type reply_markup: ReplyKeyboardMarkup|ReplyKeyboardHide|ForceReply
        :return: On success, the sent Message is returned.
        :rtype: Message
        """
        compiled_query = 'sendLocation?chat_id=%i&latitude=%f&longitude=%f&reply_to_message_id=%i' % (
            chat_id, latitude, longitude, reply_to_message_id)
        if reply_markup is not None:
            compiled_query += '&reply_markup=%s' % reply_markup.to_json()
        data = TbotPy.__urlopen(self.endpoint + compiled_query)
        return Message.build_from_json(json.loads(data)['result'])