mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-02-27 14:25:14 +01:00
Adding Travis CI
This commit is contained in:
parent
669cebd58b
commit
fdc913a580
20 changed files with 360 additions and 0 deletions
4
.travis.yml
Normal file
4
.travis.yml
Normal file
|
@ -0,0 +1,4 @@
|
|||
language: python
|
||||
python:
|
||||
- 2.7
|
||||
install: "pip install -r requirements.txt"
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
requests
|
26
telegram/__init__.py
Normal file
26
telegram/__init__.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""A library that provides a Python interface to the Telegram Bots API"""
|
||||
|
||||
__author__ = 'leandrotoledodesouza@gmail.com'
|
||||
__version__ = '0.1'
|
||||
|
||||
import json
|
||||
|
||||
from user import User
|
||||
from message import Message
|
||||
from update import Update
|
||||
from groupchat import GroupChat
|
||||
# from photosize import PhotoSize
|
||||
# from audio import Audio
|
||||
# from document import Document
|
||||
# from sticket import Sticker
|
||||
# from video import Video
|
||||
# from contact import Contact
|
||||
# from location import Location
|
||||
# from inputfile import InputFile
|
||||
# from userprofilephotos import UserProfilePhotos
|
||||
# from replykeyboardmarkup import ReplyKeyboardMarkup
|
||||
# from replykeyboardhide import ReplyKeyboardHide
|
||||
# from forcereply import ForceReply
|
||||
from bot import Bot
|
0
telegram/audio.py
Normal file
0
telegram/audio.py
Normal file
168
telegram/bot.py
Normal file
168
telegram/bot.py
Normal file
|
@ -0,0 +1,168 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""A library that provides a Python interface to the Telegram Bots API"""
|
||||
|
||||
import json
|
||||
import requests
|
||||
|
||||
from telegram import (User, Message, Update)
|
||||
|
||||
|
||||
class Bot(object):
|
||||
def __init__(self,
|
||||
token,
|
||||
base_url=None):
|
||||
|
||||
if base_url is None:
|
||||
self.base_url = 'https://api.telegram.org/bot%s' % token
|
||||
else:
|
||||
self.base_url = base_url + token
|
||||
|
||||
def getMe(self):
|
||||
|
||||
url = '%s/getMe' % (self.base_url)
|
||||
|
||||
json_data = self._requestUrl(url, 'GET')
|
||||
data = self._parseAndCheckTelegram(json_data.content)
|
||||
|
||||
return User.newFromJsonDict(data)
|
||||
|
||||
def sendMessage(self,
|
||||
chat_id,
|
||||
text,
|
||||
disable_web_page_preview=None,
|
||||
reply_to_message_id=None,
|
||||
reply_markup=None):
|
||||
|
||||
url = '%s/sendMessage' % (self.base_url)
|
||||
|
||||
data = {'chat_id': chat_id,
|
||||
'text': text}
|
||||
if disable_web_page_preview:
|
||||
data['disable_web_page_preview'] = disable_web_page_preview
|
||||
if reply_to_message_id:
|
||||
data['reply_to_message_id'] = reply_to_message_id
|
||||
if reply_markup:
|
||||
data['reply_markup'] = reply_markup
|
||||
|
||||
json_data = self._requestUrl(url, 'POST', data=data)
|
||||
data = self._parseAndCheckTelegram(json_data.content)
|
||||
|
||||
return Message.newFromJsonDict(data)
|
||||
|
||||
def forwardMessage(self,
|
||||
chat_id,
|
||||
from_chat_id,
|
||||
message_id):
|
||||
|
||||
url = '%s/forwardMessage' % (self.base_url)
|
||||
|
||||
data = {}
|
||||
if chat_id:
|
||||
data['chat_id'] = chat_id
|
||||
if from_chat_id:
|
||||
data['from_chat_id'] = from_chat_id
|
||||
if message_id:
|
||||
data['message_id'] = message_id
|
||||
|
||||
json_data = self._requestUrl(url, 'POST', data=data)
|
||||
data = self._parseAndCheckTelegram(json_data.content)
|
||||
|
||||
return Message.newFromJsonDict(data)
|
||||
|
||||
def sendPhoto(self,
|
||||
chat_id,
|
||||
photo,
|
||||
caption=None,
|
||||
reply_to_message_id=None,
|
||||
reply_markup=None):
|
||||
|
||||
url = '%s/sendPhoto' % (self.base_url)
|
||||
|
||||
data = {'chat_id': chat_id,
|
||||
'photo': photo}
|
||||
if caption:
|
||||
data['caption'] = caption
|
||||
if reply_to_message_id:
|
||||
data['reply_to_message_id'] = reply_to_message_id
|
||||
if reply_markup:
|
||||
data['reply_markup'] = reply_markup
|
||||
|
||||
json_data = self._requestUrl(url, 'POST', data=data)
|
||||
data = self._parseAndCheckTelegram(json_data.content)
|
||||
|
||||
return Message.newFromJsonDict(data)
|
||||
|
||||
def sendAudio(self):
|
||||
url = '%s/sendAudio' % (self.base_url)
|
||||
|
||||
def sendDocument(self):
|
||||
url = '%s/sendDocument' % (self.base_url)
|
||||
|
||||
def sendSticker(self):
|
||||
url = '%s/sendSticker' % (self.base_url)
|
||||
|
||||
def sendVideo(self):
|
||||
url = '%s/sendVideo' % (self.base_url)
|
||||
|
||||
def sendLocation(self):
|
||||
url = '%s/sendLocation' % (self.base_url)
|
||||
|
||||
def sendChatAction(self):
|
||||
url = '%s/sendChatAction' % (self.base_url)
|
||||
|
||||
def getUserProfilePhotos(self):
|
||||
url = '%s/getUserProfilePhotos' % (self.base_url)
|
||||
|
||||
def getUpdates(self,
|
||||
offset=None,
|
||||
limit=100,
|
||||
timeout=0):
|
||||
|
||||
url = '%s/getUpdates' % (self.base_url)
|
||||
|
||||
data = {}
|
||||
if offset:
|
||||
data['offset'] = offset
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
if timeout:
|
||||
data['timeout'] = timeout
|
||||
|
||||
json_data = self._requestUrl(url, 'POST', data=data)
|
||||
data = self._parseAndCheckTelegram(json_data.content)
|
||||
|
||||
return [Update.newFromJsonDict(x) for x in data]
|
||||
|
||||
def setWebhook(self):
|
||||
url = '%s/setWebhook' % (self.base_url)
|
||||
|
||||
def _requestUrl(self,
|
||||
url,
|
||||
method,
|
||||
data=None):
|
||||
|
||||
if method == 'POST':
|
||||
try:
|
||||
return requests.post(
|
||||
url,
|
||||
data=data
|
||||
)
|
||||
except requests.RequestException as e:
|
||||
pass
|
||||
if method == 'GET':
|
||||
try:
|
||||
return requests.get(url)
|
||||
except requests.RequestException as e:
|
||||
pass # raise TelegramError(str(e))
|
||||
return 0
|
||||
|
||||
def _parseAndCheckTelegram(self,
|
||||
json_data):
|
||||
|
||||
try:
|
||||
data = json.loads(json_data)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
return data['result']
|
0
telegram/contact.py
Normal file
0
telegram/contact.py
Normal file
0
telegram/document.py
Normal file
0
telegram/document.py
Normal file
0
telegram/forcereply.py
Normal file
0
telegram/forcereply.py
Normal file
17
telegram/groupchat.py
Normal file
17
telegram/groupchat.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
|
||||
class GroupChat(object):
|
||||
def __init__(self, **kwargs):
|
||||
param_defaults = {
|
||||
'id': None,
|
||||
'title': None
|
||||
}
|
||||
|
||||
for (param, default) in param_defaults.iteritems():
|
||||
setattr(self, param, kwargs.get(param, default))
|
||||
|
||||
@staticmethod
|
||||
def newFromJsonDict(data):
|
||||
return GroupChat(id=data.get('id', None),
|
||||
title=data.get('title', None))
|
0
telegram/inputfile.py
Normal file
0
telegram/inputfile.py
Normal file
0
telegram/location.py
Normal file
0
telegram/location.py
Normal file
100
telegram/message.py
Normal file
100
telegram/message.py
Normal file
|
@ -0,0 +1,100 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
|
||||
class Message(object):
|
||||
def __init__(self, **kwargs):
|
||||
param_defaults = {
|
||||
'message_id': None,
|
||||
'user': None,
|
||||
'date': None,
|
||||
'chat': None,
|
||||
'forward_from': None,
|
||||
'forward_date': None,
|
||||
'reply_to_message': None,
|
||||
'text': None,
|
||||
'audio': None,
|
||||
'document': None,
|
||||
'photo': None,
|
||||
'sticker': None,
|
||||
'video': None,
|
||||
'contact': None,
|
||||
'location': None,
|
||||
'new_chat_participant': None,
|
||||
'left_chat_participant': None,
|
||||
'new_chat_title': None,
|
||||
'new_chat_photo': None,
|
||||
'delete_chat_photo': None,
|
||||
'group_chat_created': None
|
||||
}
|
||||
|
||||
for (param, default) in param_defaults.iteritems():
|
||||
setattr(self, param, kwargs.get(param, default))
|
||||
|
||||
@staticmethod
|
||||
def newFromJsonDict(data):
|
||||
if 'from' in data: # from on api
|
||||
from telegram import User
|
||||
user = User.newFromJsonDict(data['from'])
|
||||
else:
|
||||
user = None
|
||||
|
||||
if 'chat' in data:
|
||||
if 'username' in data['chat']:
|
||||
from telegram import User
|
||||
chat = User.newFromJsonDict(data['chat'])
|
||||
if 'title' in data['chat']:
|
||||
from telegram import GroupChat
|
||||
chat = GroupChat.newFromJsonDict(data['chat'])
|
||||
else:
|
||||
chat = None
|
||||
|
||||
if 'forward_from' in data:
|
||||
from telegram import User
|
||||
forward_from = User.newFromJsonDict(data['forward_from'])
|
||||
else:
|
||||
forward_from = None
|
||||
|
||||
if 'reply_to_message' in data:
|
||||
reply_to_message = Message.newFromJsonDict(
|
||||
data['reply_to_message']
|
||||
)
|
||||
else:
|
||||
reply_to_message = None
|
||||
|
||||
if 'new_chat_participant' in data:
|
||||
from telegram import User
|
||||
new_chat_participant = User.newFromJsonDict(
|
||||
data['new_chat_participant']
|
||||
)
|
||||
else:
|
||||
new_chat_participant = None
|
||||
|
||||
if 'left_chat_participant' in data:
|
||||
from telegram import User
|
||||
left_chat_participant = User.newFromJsonDict(
|
||||
data['left_chat_participant']
|
||||
)
|
||||
else:
|
||||
left_chat_participant = None
|
||||
|
||||
return Message(message_id=data.get('message_id', None),
|
||||
user=user,
|
||||
date=data.get('date', None),
|
||||
chat=chat,
|
||||
forward_from=forward_from,
|
||||
forward_date=data.get('forward_date', None),
|
||||
reply_to_message=reply_to_message,
|
||||
text=data.get('text', None),
|
||||
audio=data.get('audio', None),
|
||||
document=data.get('document', None),
|
||||
photo=data.get('photo', None),
|
||||
sticker=data.get('sticker', None),
|
||||
video=data.get('video', None),
|
||||
contact=data.get('contact', None),
|
||||
location=data.get('location', None),
|
||||
new_chat_participant=new_chat_participant,
|
||||
left_chat_participant=left_chat_participant,
|
||||
new_chat_title=data.get('new_chat_title', None),
|
||||
new_chat_photo=data.get('new_chat_photo', None),
|
||||
delete_chat_photo=data.get('delete_chat_photo', None),
|
||||
group_chat_created=data.get('group_chat_created', None))
|
0
telegram/photosize.py
Normal file
0
telegram/photosize.py
Normal file
0
telegram/replykeyboardhide.py
Normal file
0
telegram/replykeyboardhide.py
Normal file
0
telegram/replykeyboardmarkup.py
Normal file
0
telegram/replykeyboardmarkup.py
Normal file
0
telegram/sticker.py
Normal file
0
telegram/sticker.py
Normal file
23
telegram/update.py
Normal file
23
telegram/update.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
|
||||
class Update(object):
|
||||
def __init__(self, **kwargs):
|
||||
param_defaults = {
|
||||
'update_id': None,
|
||||
'message': None
|
||||
}
|
||||
|
||||
for (param, default) in param_defaults.iteritems():
|
||||
setattr(self, param, kwargs.get(param, default))
|
||||
|
||||
@staticmethod
|
||||
def newFromJsonDict(data):
|
||||
if 'message' in data:
|
||||
from telegram import Message
|
||||
message = Message.newFromJsonDict(data['message'])
|
||||
else:
|
||||
message = None
|
||||
|
||||
return Update(update_id=data.get('update_id', None),
|
||||
message=message)
|
21
telegram/user.py
Normal file
21
telegram/user.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
|
||||
class User(object):
|
||||
def __init__(self, **kwargs):
|
||||
param_defaults = {
|
||||
'id': None,
|
||||
'first_name': None,
|
||||
'last_name': None,
|
||||
'username': None
|
||||
}
|
||||
|
||||
for (param, default) in param_defaults.iteritems():
|
||||
setattr(self, param, kwargs.get(param, default))
|
||||
|
||||
@staticmethod
|
||||
def newFromJsonDict(data):
|
||||
return User(id=data.get('id', None),
|
||||
first_name=data.get('first_name', None),
|
||||
last_name=data.get('last_name', None),
|
||||
username=data.get('username', None))
|
0
telegram/userprofilephotos.py
Normal file
0
telegram/userprofilephotos.py
Normal file
0
telegram/video.py
Normal file
0
telegram/video.py
Normal file
Loading…
Add table
Reference in a new issue