Commit 1ca6a604 authored by Hermann Krumrey's avatar Hermann Krumrey

Merge branch 'develop' into 'master'

Develop

See merge request !2
parents b99e630f aec12792
V 0.2.0:
- Implement message reply mechanisms
- Add prompt mechanisms to Settings
V 0.1.1:
- Enabled directly deserializing settings into connections
V 0.1.0:
- Initial Release
\ No newline at end of file
......@@ -19,7 +19,7 @@ LICENSE"""
import time
import logging
from typing import Callable, List
from typing import Callable, List, Type
from bokkichat.entities.Address import Address
from bokkichat.entities.message.Message import Message
from bokkichat.settings.Settings import Settings
......@@ -39,7 +39,7 @@ class Connection:
:param settings: The settings for the connection
"""
self.settings = settings
self.logger = logging.getLogger("bokkichat")
self.logger = logging.getLogger(self.__class__.__name__)
self.looping = False
self.loop_break = False
......@@ -51,6 +51,14 @@ class Connection:
"""
raise NotImplementedError()
@staticmethod
def settings_cls() -> Type[Settings]:
"""
The settings class used by this connection
:return: The settings class
"""
raise NotImplementedError()
def send(self, message: Message):
"""
Sends a message. A message may be either a TextMessage
......@@ -96,3 +104,13 @@ class Connection:
:return: None
"""
raise NotImplementedError()
@classmethod
def from_serialized_settings(cls, serialized: str) -> "Connection":
"""
Generates a Connection using serialized settings
:param serialized: The serialized settings
:return: The generated connection
"""
settings = cls.settings_cls().deserialize(serialized)
return cls(settings)
......@@ -17,11 +17,12 @@ You should have received a copy of the GNU General Public License
along with bokkichat. If not, see <http://www.gnu.org/licenses/>.
LICENSE"""
from typing import List
from typing import List, Type
from bokkichat.entities.Address import Address
from bokkichat.entities.message.Message import Message
from bokkichat.entities.message.TextMessage import TextMessage
from bokkichat.connection.Connection import Connection
from bokkichat.settings.impl.CliSettings import CliSettings
class CliConnection(Connection):
......@@ -38,6 +39,14 @@ class CliConnection(Connection):
"""
return Address("CLI")
@staticmethod
def settings_cls() -> Type[CliSettings]:
"""
The settings class used by this connection
:return: The settings class
"""
return CliSettings
# noinspection PyMethodMayBeStatic
def send(self, message: Message):
"""
......
......@@ -20,7 +20,7 @@ LICENSE"""
# noinspection PyPackageRequirements
import telegram
import requests
from typing import List, Dict, Any, Optional
from typing import List, Dict, Any, Optional, Type
from bokkichat.entities.Address import Address
from bokkichat.entities.message.Message import Message
from bokkichat.entities.message.TextMessage import TextMessage
......@@ -58,6 +58,14 @@ class TelegramBotConnection(Connection):
"""
return Address(str(self.bot.name))
@staticmethod
def settings_cls() -> Type[TelegramBotSettings]:
"""
The settings class used by this connection
:return: The settings class
"""
return TelegramBotSettings
def send(self, message: Message):
"""
Sends a message. A message may be either a TextMessage
......
......@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with bokkichat. If not, see <http://www.gnu.org/licenses/>.
LICENSE"""
from typing import Optional
from bokkichat.entities.message.Message import Message
from bokkichat.entities.Address import Address
from bokkichat.entities.message.MediaType import MediaType
......@@ -54,3 +55,33 @@ class MediaMessage(Message):
:return: A string representation of the MediaMessage object
"""
return "{}: {}".format(self.media_type.name, self.caption)
def make_reply(
self,
media_type: Optional[MediaType] = None,
data: Optional[bytes] = None,
caption: Optional[str] = None
) -> Message:
"""
Swaps the sender and receiver of the message
:param media_type: The type of the contained media
:param data: The data of the attached media
:param caption: The caption attached to the media
:return: The generated reply
"""
if media_type is None:
media_type = self.media_type
if data is None:
data = self.data
if caption is None:
caption = self.caption
return MediaMessage(
self.receiver, self.sender, media_type, data, caption
)
@staticmethod
def is_media() -> bool:
"""
:return: Whether or not the message is a media message
"""
return True
......@@ -39,3 +39,24 @@ class Message:
:return: A string representation of the Message object
"""
raise NotImplementedError()
def make_reply(self) -> "Message":
"""
Swaps the sender and receiver of the message
:return: The generated reply
"""
raise NotImplementedError()
@staticmethod
def is_text() -> bool:
"""
:return: Whether or not the message is a text message
"""
return False
@staticmethod
def is_media() -> bool:
"""
:return: Whether or not the message is a media message
"""
return False
......@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with bokkichat. If not, see <http://www.gnu.org/licenses/>.
LICENSE"""
from typing import Optional
from bokkichat.entities.message.Message import Message
from bokkichat.entities.Address import Address
......@@ -34,7 +35,7 @@ class TextMessage(Message):
sender: Address,
receiver: Address,
body: str,
title: str = ""
title: Optional[str] = ""
):
"""
Initializes the TextMessage object
......@@ -52,3 +53,25 @@ class TextMessage(Message):
:return: A string representation of the TextMessage object
"""
return "{}: {}".format(self.title, self.body)
def make_reply(
self,
body: Optional[str] = None,
title: Optional[str] = None
) -> Message:
"""
Swaps the sender and receiver of the message
:return: The generated reply
"""
if body is None:
body = self.body
if title is None:
title = self.title
return TextMessage(self.receiver, self.sender, body, title)
@staticmethod
def is_text() -> bool:
"""
:return: Whether or not the message is a text message
"""
return True
......@@ -32,10 +32,30 @@ class Settings:
raise NotImplementedError()
@classmethod
def deserialize(cls, serialized: str):
def deserialize(cls, serialized: str) -> "Settings":
"""
Deserializes a string and generates a Settings object from it
:param serialized: The serialized string
:return: The deserialized Settings object
"""
raise NotImplementedError()
@classmethod
def prompt(cls) -> "Settings":
"""
Prompts the user for input to generate a Settings object
:return: The generated settings object
"""
raise NotImplementedError()
@staticmethod
def user_input(prompt: str) -> str:
"""
Prompts the user for input
:param prompt: The text to display with the prompt
:return: The resulting response
"""
resp = ""
while not resp:
resp = input(prompt + ": ")
return resp
......@@ -34,10 +34,18 @@ class CliSettings(Settings):
return ""
@classmethod
def deserialize(cls, _: str):
def deserialize(cls, _: str) -> "CliSettings":
"""
Deserializes a string and generates a Settings object from it
:param _: The serialized string
:return: The deserialized Settings object
"""
return cls()
@classmethod
def prompt(cls) -> Settings:
"""
Prompts the user for input to generate a Settings object
:return: The generated settings object
"""
return CliSettings()
......@@ -44,7 +44,7 @@ class TelegramBotSettings(Settings):
})
@classmethod
def deserialize(cls, serialized: str):
def deserialize(cls, serialized: str) -> "TelegramBotSettings":
"""
Deserializes a string and generates a Settings object from it
:param serialized: The serialized string
......@@ -52,3 +52,12 @@ class TelegramBotSettings(Settings):
"""
obj = json.loads(serialized)
return cls(obj["api_key"])
@classmethod
def prompt(cls) -> Settings:
"""
Prompts the user for input to generate a Settings object
:return: The generated settings object
"""
api_key = cls.user_input("API Key")
return cls(api_key=api_key)
0.1.0
\ No newline at end of file
0.2.0
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment