1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Compare commits

..

10 Commits
0.3.4 ... 0.3.6

Author SHA1 Message Date
f4b0b61b9d Update version.
Change log:
- Support File type.
- New method getFile supported.
2015-09-19 23:06:47 +08:00
4b8989f3d2 Merge pull request #74 from eternnoir/develop
Add getFile method and File type
2015-09-19 10:55:44 +08:00
19b4e35ee5 Attempt to fix failing test case 2015-09-18 21:11:56 +02:00
61b4ca8a37 Added download_file and created an example 2015-09-18 20:54:22 +02:00
1038d4fafa Added download_file 2015-09-18 20:53:10 +02:00
fd1f16598b Added File & getFile, including testing 2015-09-18 20:31:29 +02:00
07c28830db Update version.
Change log:
- Hotfix handler lambda bug.
2015-09-10 21:51:51 +08:00
a8fccdbeb3 Add test. 2015-09-10 21:44:14 +08:00
c213b7732b Add lambda text. 2015-09-10 21:38:09 +08:00
2ca5c0d6f3 Hotfix lambda bug. 2015-09-10 21:27:37 +08:00
7 changed files with 106 additions and 10 deletions

View File

@ -251,6 +251,16 @@ tb.send_location(chat_id, lat, lon)
# action_string can be one of the following strings: 'typing', 'upload_photo', 'record_video', 'upload_video',
# 'record_audio', 'upload_audio', 'upload_document' or 'find_location'.
tb.send_chat_action(chat_id, action_string)
# getFile
# Downloading a file is straightforward
# Returns a File object
import requests
file_info = tb.get_file(file_id)
file = requests.get('https://api.telegram.org/file/bot{0}/{1}'.format(API_TOKEN, file_info.file_path))
```
#### Reply markup
All `send_xyz` functions of TeleBot take an optional `reply_markup` argument. This argument must be an instance of `ReplyKeyboardMarkup`, `ReplyKeyboardHide` or `ForceReply`, which are defined in types.py.

View File

@ -0,0 +1,15 @@
import telebot
TOKEN = 'YOUR BOT TOKEN'
CHAT_ID = 'YOUR CHAT ID'
bot = telebot.TeleBot(TOKEN)
ret_msg = bot.send_voice(CHAT_ID, open('tests/test_data/record.ogg'))
file_info = bot.get_file(ret_msg.voice.file_id)
downloaded_file = bot.download_file(file_info.file_path)
with open('new_file.ogg', 'wb') as new_file:
new_file.write(downloaded_file)

View File

@ -6,7 +6,7 @@ def readme():
return f.read()
setup(name='pyTelegramBotAPI',
version='0.3.4',
version='0.3.6',
description='Python Telegram bot api. ',
long_description=readme(),
author='eternnoir',

View File

@ -22,10 +22,6 @@ from telebot import apihelper, types, util
"""
Module : telebot
"""
API_URL = r"https://api.telegram.org/"
class TeleBot:
""" This is TeleBot Class
Methods:
@ -169,6 +165,12 @@ class TeleBot:
result = apihelper.get_me(self.token)
return types.User.de_json(result)
def get_file(self, file_id):
return types.File.de_json(apihelper.get_file(self.token, file_id))
def download_file(self, file_path):
return apihelper.download_file(self.token, file_path)
def get_user_profile_photos(self, user_id, offset=None, limit=None):
"""
Retrieves the user profile photos of the person with 'user_id'
@ -434,7 +436,7 @@ class TeleBot:
return message.content_type == 'text' and re.search(filter_value, message.text)
if filter == 'commands':
return message.content_type == 'text' and util.extract_command(message.text) in filter_value
if filter == 'func':
if filter == 'lambda':
return filter_value(message)
return False

View File

@ -7,8 +7,11 @@ from telebot import util
logger = telebot.logger
API_URL = "https://api.telegram.org/bot{0}/{1}"
FILE_URL = "https://api.telegram.org/file/bot{0}/{1}"
def _make_request(token, method_name, method='get', params=None, files=None):
def _make_request(token, method_name, method='get', params=None, files=None, base_url=API_URL):
"""
Makes a request to the Telegram API.
:param token: The bot's API token. (Created with @BotFather)
@ -18,10 +21,10 @@ def _make_request(token, method_name, method='get', params=None, files=None):
:param files: Optional files.
:return: The result parsed to a JSON dictionary.
"""
request_url = telebot.API_URL + 'bot' + token + '/' + method_name
request_url = base_url.format(token, method_name)
logger.debug("Request: method={0} url={1} params={2} files={3}".format(method, request_url, params, files))
result = requests.request(method, request_url, params=params, files=files)
logger.debug("The server returned: '{0}'".format(result.text))
logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
return _check_result(method_name, result)['result']
@ -62,6 +65,21 @@ def get_me(token):
return _make_request(token, method_url)
def get_file(token, file_id):
method_url = 'getFile'
return _make_request(token, method_url, params={'file_id': file_id})
def download_file(token, file_path):
url = FILE_URL.format(token, file_path)
result = requests.get(url)
if result.status_code != 200:
msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]'\
.format(result.status_code, result.reason, result.text)
raise ApiException(msg, 'Download file', result)
return result.content
def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None,
parse_mode=None):
"""

View File

@ -408,6 +408,26 @@ class UserProfilePhotos(JsonDeserializable):
self.total_count = total_count
self.photos = photos
class File(JsonDeserializable):
@classmethod
def de_json(cls, json_type):
obj = cls.check_json(json_type)
file_id = obj['file_id']
file_size = None
file_path = None
if 'file_size' in obj:
file_size = obj['file_size']
if 'file_path' in obj:
file_path = obj['file_path']
return File(file_id, file_size, file_path)
def __init__(self, file_id, file_size, file_path):
self.file_id = file_id
self.file_size = file_size
self.file_path = file_path
class ForceReply(JsonSerializable):
def __init__(self, selective=None):

View File

@ -9,7 +9,6 @@ import os
import telebot
from telebot import types
from telebot import apihelper
from telebot import util
should_skip = 'TOKEN' and 'CHAT_ID' not in os.environ
@ -56,6 +55,30 @@ class TestTeleBot:
time.sleep(1)
assert msg.text == 'got'
def test_message_handler_lambda(self):
bot = telebot.TeleBot('')
msg = self.create_text_message(r'lambda_text')
@bot.message_handler(func=lambda message: r'lambda' in message.text)
def command_url(message):
msg.text = 'got'
bot.process_new_messages([msg])
time.sleep(1)
assert msg.text == 'got'
def test_message_handler_lambda_fail(self):
bot = telebot.TeleBot('')
msg = self.create_text_message(r'text')
@bot.message_handler(func=lambda message: r'lambda' in message.text)
def command_url(message):
msg.text = 'got'
bot.process_new_messages([msg])
time.sleep(1)
assert not msg.text == 'got'
def test_message_handler_reg_fail(self):
bot = telebot.TeleBot('')
msg = self.create_text_message(r'web.telegram.org/')
@ -132,6 +155,14 @@ class TestTeleBot:
ret_msg = tb.send_voice(CHAT_ID, file_data)
assert ret_msg.voice.mime_type == 'audio/ogg'
def test_get_file(self):
file_data = open('./test_data/record.ogg', 'rb')
tb = telebot.TeleBot(TOKEN)
ret_msg = tb.send_voice(CHAT_ID, file_data)
file_id = ret_msg.voice.file_id
file_info = tb.get_file(file_id)
assert file_info.file_id == file_id
def test_send_message(self):
text = 'CI Test Message'
tb = telebot.TeleBot(TOKEN)