From 53c44f14bd24ec0a1b4d4eba83543ae44cfd30b1 Mon Sep 17 00:00:00 2001 From: ErgoZ Date: Thu, 10 Sep 2015 20:15:20 +0300 Subject: [PATCH] Add Markdown support for sendMessage method. --- README.rst | 4 ++++ telegram/__init__.py | 3 ++- telegram/bot.py | 7 +++++++ telegram/parsemode.py | 26 ++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 telegram/parsemode.py diff --git a/README.rst b/README.rst index 53c3f9795..75af2a814 100644 --- a/README.rst +++ b/README.rst @@ -189,6 +189,10 @@ To post a text message:: >>> bot.sendMessage(chat_id=chat_id, text="I'm sorry Dave I'm afraid I can't do that.") +To post a text message with markdown:: + + >>> bot.sendMessage(chat_id=chat_id, text="*bold* _italic_ [link](http://google.com).", parse_mode=telegram.ParseMode.MARKDOWN) + To post an Emoji (special thanks to `Tim Whitlock `_):: >>> bot.sendMessage(chat_id=chat_id, text=telegram.Emoji.PILE_OF_POO) diff --git a/telegram/__init__.py b/telegram/__init__.py index 834b4675f..178e80cc0 100644 --- a/telegram/__init__.py +++ b/telegram/__init__.py @@ -42,6 +42,7 @@ from .error import TelegramError from .inputfile import InputFile from .nullhandler import NullHandler from .emoji import Emoji +from .parsemode import ParseMode from .message import Message from .update import Update from .bot import Bot @@ -50,5 +51,5 @@ __all__ = ['Bot', 'Emoji', 'TelegramError', 'InputFile', 'ReplyMarkup', 'ForceReply', 'ReplyKeyboardHide', 'ReplyKeyboardMarkup', 'UserProfilePhotos', 'ChatAction', 'Location', 'Contact', 'Video', 'Sticker', 'Document', 'Audio', 'PhotoSize', 'GroupChat', - 'Update', 'Message', 'User', 'TelegramObject', 'NullHandler', + 'Update', 'ParseMode', 'Message', 'User', 'TelegramObject', 'NullHandler', 'Voice'] diff --git a/telegram/bot.py b/telegram/bot.py index 7429f9e07..792ae14b6 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -181,6 +181,7 @@ class Bot(TelegramObject): def sendMessage(self, chat_id, text, + parse_mode=None, disable_web_page_preview=None, **kwargs): """Use this method to send text messages. @@ -189,6 +190,10 @@ class Bot(TelegramObject): chat_id: Unique identifier for the message recipient - telegram.User or telegram.GroupChat id. + 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. [Optional] text: Text of the message to be sent. disable_web_page_preview: @@ -209,6 +214,8 @@ class Bot(TelegramObject): data = {'chat_id': chat_id, 'text': text} + if parse_mode: + data['parse_mode'] = parse_mode if disable_web_page_preview: data['disable_web_page_preview'] = disable_web_page_preview diff --git a/telegram/parsemode.py b/telegram/parsemode.py new file mode 100644 index 000000000..897693acd --- /dev/null +++ b/telegram/parsemode.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# pylint: disable=R0903 +# +# A library that provides a Python interface to the Telegram Bot API +# Copyright (C) 2015 Leandro Toledo de Souza +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser Public License for more details. +# +# You should have received a copy of the GNU Lesser Public License +# along with this program. If not, see [http://www.gnu.org/licenses/]. + +"""This module contains a object that represents a Telegram Message Parse Modes""" + + +class ParseMode(object): + """This object represents a Telegram Message Parse Modes.""" + + MARKDOWN = 'Markdown'