mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 14:35:00 +01:00
base class for telegram class
This commit is contained in:
parent
5460671384
commit
ce852de9a8
15 changed files with 65 additions and 68 deletions
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class Audio(object):
|
||||
class Audio(Base):
|
||||
def __init__(self,
|
||||
file_id,
|
||||
duration,
|
||||
|
@ -30,6 +31,3 @@ class Audio(object):
|
|||
if self.file_size:
|
||||
json_data['file_size'] = self.file_size
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class Contact(object):
|
||||
class Contact(Base):
|
||||
def __init__(self,
|
||||
phone_number,
|
||||
first_name,
|
||||
|
@ -30,6 +31,3 @@ class Contact(object):
|
|||
if self.user_id:
|
||||
json_data['user_id'] = self.user_id
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class Document(object):
|
||||
class Document(Base):
|
||||
def __init__(self,
|
||||
file_id,
|
||||
thumb,
|
||||
|
@ -41,6 +42,3 @@ class Document(object):
|
|||
if self.file_size:
|
||||
json_data['file_size'] = self.file_size
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -22,6 +22,3 @@ class ForceReply(ReplyMarkup):
|
|||
if self.selective:
|
||||
json_data['selective'] = self.selective
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class GroupChat(object):
|
||||
class GroupChat(Base):
|
||||
def __init__(self,
|
||||
id,
|
||||
title):
|
||||
|
@ -20,6 +21,3 @@ class GroupChat(object):
|
|||
json_data = {'id': self.id,
|
||||
'title': self.title}
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class Location(object):
|
||||
class Location(Base):
|
||||
def __init__(self,
|
||||
longitude,
|
||||
latitude):
|
||||
|
@ -20,6 +21,3 @@ class Location(object):
|
|||
json_data = {'longitude': self.longitude,
|
||||
'latitude': self.latitude}
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class Message(object):
|
||||
class Message(Base):
|
||||
def __init__(self,
|
||||
message_id,
|
||||
from_user,
|
||||
|
@ -203,6 +204,3 @@ class Message(object):
|
|||
if self.group_chat_created:
|
||||
json_data['group_chat_created'] = self.group_chat_created
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class PhotoSize(object):
|
||||
class PhotoSize(Base):
|
||||
def __init__(self,
|
||||
file_id,
|
||||
width,
|
||||
|
@ -29,6 +30,3 @@ class PhotoSize(object):
|
|||
if self.file_size:
|
||||
json_data['file_size'] = self.file_size
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class ReplyMarkup(object):
|
||||
class ReplyMarkup(Base):
|
||||
def to_json(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class Sticker(object):
|
||||
class Sticker(Base):
|
||||
def __init__(self,
|
||||
file_id,
|
||||
width,
|
||||
|
@ -39,6 +40,3 @@ class Sticker(object):
|
|||
if self.file_size:
|
||||
json_data['file_size'] = self.file_size
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
26
telegram/telegram_boject_base.py
Normal file
26
telegram/telegram_boject_base.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
from abc import ABCMeta, abstractmethod
|
||||
from telegram import TelegramError
|
||||
|
||||
|
||||
class Base(object):
|
||||
|
||||
"""Base class for most telegram object"""
|
||||
|
||||
__metaclass__ = ABCMeta
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
||||
def __getitem__(self, item):
|
||||
try:
|
||||
return self.__dict__[item]
|
||||
except KeyError as e:
|
||||
raise TelegramError(str(e))
|
||||
|
||||
@staticmethod
|
||||
def de_json(data):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def to_json(self):
|
||||
pass
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class Update(object):
|
||||
class Update(Base):
|
||||
def __init__(self,
|
||||
update_id,
|
||||
message=None):
|
||||
|
@ -27,6 +28,3 @@ class Update(object):
|
|||
if self.message:
|
||||
json_data['message'] = self.message.to_json()
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class User(object):
|
||||
class User(Base):
|
||||
def __init__(self,
|
||||
id,
|
||||
first_name,
|
||||
|
@ -38,6 +39,3 @@ class User(object):
|
|||
if self.username:
|
||||
json_data['username'] = self.username
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class UserProfilePhotos(object):
|
||||
class UserProfilePhotos(Base):
|
||||
def __init__(self,
|
||||
total_count,
|
||||
photos):
|
||||
|
@ -33,6 +34,3 @@ class UserProfilePhotos(object):
|
|||
for photo in self.photos:
|
||||
json_data['photos'].append([x.to_json() for x in photo])
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
|
@ -2,9 +2,10 @@
|
|||
|
||||
|
||||
import json
|
||||
from .telegram_boject_base import Base
|
||||
|
||||
|
||||
class Video(object):
|
||||
class Video(Base):
|
||||
def __init__(self,
|
||||
file_id,
|
||||
width,
|
||||
|
@ -53,6 +54,3 @@ class Video(object):
|
|||
if self.caption:
|
||||
json_data['caption'] = self.caption
|
||||
return json.dumps(json_data)
|
||||
|
||||
def __str__(self):
|
||||
return self.to_json()
|
||||
|
|
Loading…
Reference in a new issue