X Tutup
Skip to content

Commit f7a1530

Browse files
committed
Adding sendPhoto method
1 parent 8fb2bf5 commit f7a1530

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var/
2222
*.egg-info/
2323
.installed.cfg
2424
*.egg
25+
.env
2526

2627
# PyInstaller
2728
# Usually these files are written by a python script from a template

telegram/bot.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def sendPhoto(self,
8181

8282
data = {'chat_id': chat_id,
8383
'photo': photo}
84+
8485
if caption:
8586
data['caption'] = caption
8687
if reply_to_message_id:
@@ -143,13 +144,25 @@ def _requestUrl(self,
143144
data=None):
144145

145146
if method == 'POST':
146-
try:
147-
return requests.post(
148-
url,
149-
data=data
150-
)
151-
except requests.RequestException as e:
152-
pass
147+
if data.has_key('photo'):
148+
try:
149+
photo = data.pop('photo')
150+
151+
return requests.post(
152+
url,
153+
data=data,
154+
files={'photo': photo}
155+
)
156+
except requests.RequestException as e:
157+
pass
158+
else:
159+
try:
160+
return requests.post(
161+
url,
162+
data=data
163+
)
164+
except requests.RequestException as e:
165+
pass
153166
if method == 'GET':
154167
try:
155168
return requests.get(url)

telegram/inputfile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env python
2+
3+
4+
class InputFile(object):
5+
pass

tests/telegram.png

12.6 KB
Loading

tests/test_bot.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def testForwardMessage(self):
4444
self.assertEqual(u'Oi', message.text)
4545
self.assertEqual(u'leandrotoledo', message.forward_from.username)
4646

47-
# def testSendPhoto(self):
48-
# '''Test the telegram.Bot sendPhoto method'''
49-
# print 'Testing sendPhoto'
50-
# message = self._bot.sendPhoto()
47+
def testSendPhoto(self):
48+
'''Test the telegram.Bot sendPhoto method'''
49+
print 'Testing sendPhoto'
50+
message = self._bot.sendPhoto(photo=open('tests/telegram.png', 'rb'),
51+
chat_id=12173560)
52+
#self.assertEqual(u'')

0 commit comments

Comments
 (0)
X Tutup