Skip to content

Commit 51b848f

Browse files
committed
Urlparse fix for py2
1 parent be20c26 commit 51b848f

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

examples/echobot2.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
Press Ctrl-C on the command line or send a signal to the process to stop the
1616
bot.
1717
"""
18+
from pprint import pprint
19+
20+
from telegram.utils import helpers
1821

1922
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
2023
import logging
@@ -37,7 +40,7 @@ def help(bot, update):
3740

3841

3942
def echo(bot, update):
40-
update.message.reply_text(update.message.text)
43+
update.message.reply_text(helpers.extract_urls(update.message))
4144

4245

4346
def error(bot, update, error):
@@ -46,7 +49,7 @@ def error(bot, update, error):
4649

4750
def main():
4851
# Create the EventHandler and pass it your bot's token.
49-
updater = Updater("TOKEN")
52+
updater = Updater("324133401:AAHVjjXotCDXC_kIIkfM0O6bm9-l7BfJw-I")
5053

5154
# Get the dispatcher to register handlers
5255
dp = updater.dispatcher
@@ -56,14 +59,22 @@ def main():
5659
dp.add_handler(CommandHandler("help", help))
5760

5861
# on noncommand i.e message - echo the message on Telegram
59-
dp.add_handler(MessageHandler(Filters.text, echo))
62+
dp.add_handler(MessageHandler(Filters.photo, echo))
6063

6164
# log all errors
6265
dp.add_error_handler(error)
6366

6467
# Start the Bot
6568
updater.start_polling()
6669

70+
urls = "http://google.com and http://github.com/ and python-telegram-bot.readthedocs.io/en/latest/"
71+
result = helpers._extract_urls_from_text(urls)
72+
pprint(result)
73+
assert len(result) == 3
74+
assert result[0] == 'http://google.com'
75+
assert result[1] == 'http://github.com/'
76+
assert result[2] == 'python-telegram-bot.readthedocs.io/en/latest/'
77+
6778
# Run the bot until you press Ctrl-C or the process receives SIGINT,
6879
# SIGTERM or SIGABRT. This should be used most of the time, since
6980
# start_polling() is non-blocking and will stop the bot gracefully.

telegram/utils/helpers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
import re
2222
from collections import OrderedDict
2323
from datetime import datetime
24-
from urllib.parse import urlparse
24+
try:
25+
from urllib.parse import urlparse
26+
except ImportError:
27+
from urlparse import urlparse
2528

2629
try:
2730
from html import escape as escape_html # noqa: F401

0 commit comments

Comments
 (0)