Compare commits
2 Commits
a896fbe38a
...
177ec2c783
Author | SHA1 | Date | |
---|---|---|---|
177ec2c783 | |||
13097b36fb |
@ -40,11 +40,15 @@ jobs:
|
|||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: uv sync
|
run: uv sync
|
||||||
|
|
||||||
|
# - name: Lint
|
||||||
|
# run: |
|
||||||
|
# uv run pylint --fail-under=8 --recursive=yes --output-format=parseable --output=lintreport.txt app/ tests/
|
||||||
|
# cat lintreport.txt
|
||||||
|
|
||||||
- name: Lint
|
- name: Lint
|
||||||
run: |
|
run: |
|
||||||
uv run pylint --fail-under=8 --recursive=yes --output-format=parseable app/ tests/ # --output=lintreport.txt
|
uv run pylint --fail-under=8 --recursive=yes --output-format=parseable app/ tests/
|
||||||
cat lintreport.txt
|
|
||||||
|
|
||||||
- name: Unit Test
|
- name: Unit Test
|
||||||
run: |
|
run: |
|
||||||
|
14
app/close.py
14
app/close.py
@ -1,8 +1,13 @@
|
|||||||
|
"""Command module for handling the 'exit' command in the Webex meme bot."""
|
||||||
|
|
||||||
from webex_bot.models.command import Command
|
from webex_bot.models.command import Command
|
||||||
|
|
||||||
|
|
||||||
class ExitCommand(Command):
|
class ExitCommand(Command):
|
||||||
|
"""Command to handle the 'exit' command in the Webex meme bot."""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
"""Initialize the ExitCommand with command keyword and help message."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
command_keyword="exit",
|
command_keyword="exit",
|
||||||
help_message="Exit",
|
help_message="Exit",
|
||||||
@ -10,11 +15,14 @@ class ExitCommand(Command):
|
|||||||
)
|
)
|
||||||
self.sender: str = ""
|
self.sender: str = ""
|
||||||
|
|
||||||
def pre_execute(self, message, attachment_actions, activity) -> None:
|
def pre_execute(self, message, attachment_actions, activity) -> None: # pylint: disable=unused-argument
|
||||||
|
"""Pre-execution logic for the exit command."""
|
||||||
return
|
return
|
||||||
|
|
||||||
def execute(self, message, attachment_actions, activity) -> None:
|
def execute(self, message, attachment_actions, activity) -> None: # pylint: disable=unused-argument
|
||||||
|
"""Execute the exit command."""
|
||||||
return
|
return
|
||||||
|
|
||||||
def post_execute(self, message, attachment_actions, activity) -> None:
|
def post_execute(self, message, attachment_actions, activity) -> None: # pylint: disable=unused-argument
|
||||||
|
"""Post-execution logic for the exit command."""
|
||||||
return
|
return
|
||||||
|
29
app/img.py
29
app/img.py
@ -1,3 +1,5 @@
|
|||||||
|
"""Generates meme images using the memegen.link API."""
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
CHAR_REPLACEMENTS: list = [
|
CHAR_REPLACEMENTS: list = [
|
||||||
@ -17,6 +19,11 @@ CHAR_REPLACEMENTS: list = [
|
|||||||
|
|
||||||
|
|
||||||
def get_templates() -> list[dict]:
|
def get_templates() -> list[dict]:
|
||||||
|
"""Fetches available meme templates from the memegen.link API.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
list[dict]: A list of dictionaries containing meme template information.
|
||||||
|
"""
|
||||||
url: str = "https://api.memegen.link/templates"
|
url: str = "https://api.memegen.link/templates"
|
||||||
req: requests.Response = requests.get(url=url, timeout=10)
|
req: requests.Response = requests.get(url=url, timeout=10)
|
||||||
req.raise_for_status()
|
req.raise_for_status()
|
||||||
@ -40,6 +47,14 @@ def get_templates() -> list[dict]:
|
|||||||
|
|
||||||
|
|
||||||
def format_meme_string(input_string: str) -> str:
|
def format_meme_string(input_string: str) -> str:
|
||||||
|
"""Formats a string for use in a meme image URL.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
input_string (str): The string to format.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The formatted string suitable for meme image URLs.
|
||||||
|
"""
|
||||||
# https://memegen.link/#special-characters
|
# https://memegen.link/#special-characters
|
||||||
out_string: str = input_string
|
out_string: str = input_string
|
||||||
for char_replacement in CHAR_REPLACEMENTS:
|
for char_replacement in CHAR_REPLACEMENTS:
|
||||||
@ -48,6 +63,16 @@ def format_meme_string(input_string: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def generate_api_url(template: str, top_str: str, btm_str: str) -> str:
|
def generate_api_url(template: str, top_str: str, btm_str: str) -> str:
|
||||||
|
"""Generates a meme image URL using the memegen.link API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
template (str): The template identifier in the format "name.ext".
|
||||||
|
top_str (str): The text for the top line of the meme.
|
||||||
|
btm_str (str): The text for the bottom line of the meme.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The complete URL for the meme image.
|
||||||
|
"""
|
||||||
tmpl_name: str
|
tmpl_name: str
|
||||||
tmpl_ext: str
|
tmpl_ext: str
|
||||||
tmpl_name, tmpl_ext = template.split(".")
|
tmpl_name, tmpl_ext = template.split(".")
|
||||||
@ -55,7 +80,5 @@ def generate_api_url(template: str, top_str: str, btm_str: str) -> str:
|
|||||||
top_str = format_meme_string(top_str)
|
top_str = format_meme_string(top_str)
|
||||||
btm_str = format_meme_string(btm_str)
|
btm_str = format_meme_string(btm_str)
|
||||||
|
|
||||||
url: str = (
|
url: str = f"https://api.memegen.link/images/{tmpl_name}/{top_str}/{btm_str}.{tmpl_ext}"
|
||||||
f"https://api.memegen.link/images/{tmpl_name}/{top_str}/{btm_str}.{tmpl_ext}"
|
|
||||||
)
|
|
||||||
return url
|
return url
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#!/usr/local/bin/python3
|
#!/usr/local/bin/python3
|
||||||
|
|
||||||
|
"""Main entry point for the Webex Bot application."""
|
||||||
|
|
||||||
from webex_bot.webex_bot import WebexBot
|
from webex_bot.webex_bot import WebexBot
|
||||||
|
|
||||||
from app import close, meme
|
from app import close, meme
|
||||||
@ -18,6 +20,7 @@ def create_bot() -> WebexBot:
|
|||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
|
"""Main function to run the Webex Bot."""
|
||||||
bot: WebexBot = create_bot()
|
bot: WebexBot = create_bot()
|
||||||
bot.add_command(meme.MakeMemeCommand())
|
bot.add_command(meme.MakeMemeCommand())
|
||||||
bot.add_command(close.ExitCommand())
|
bot.add_command(close.ExitCommand())
|
||||||
|
32
app/meme.py
32
app/meme.py
@ -1,3 +1,5 @@
|
|||||||
|
"""Generates meme images using the memegen.link API."""
|
||||||
|
|
||||||
from webex_bot.models.command import Command
|
from webex_bot.models.command import Command
|
||||||
from webex_bot.models.response import Response, response_from_adaptive_card
|
from webex_bot.models.response import Response, response_from_adaptive_card
|
||||||
from webexteamssdk.models.cards import (
|
from webexteamssdk.models.cards import (
|
||||||
@ -22,6 +24,7 @@ class MakeMemeCommand(Command):
|
|||||||
"""Class for initial Webex interactive card."""
|
"""Class for initial Webex interactive card."""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
"""Initialize the MakeMemeCommand with command keyword and help message."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
command_keyword="/meme",
|
command_keyword="/meme",
|
||||||
help_message="Make a Meme",
|
help_message="Make a Meme",
|
||||||
@ -29,10 +32,12 @@ class MakeMemeCommand(Command):
|
|||||||
delete_previous_message=True,
|
delete_previous_message=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
def pre_execute(self, message, attachment_actions, activity) -> None:
|
def pre_execute(self, message, attachment_actions, activity) -> None: # pylint: disable=unused-argument
|
||||||
|
"""Pre-execution logic for the MakeMemeCommand."""
|
||||||
return
|
return
|
||||||
|
|
||||||
def execute(self, message, attachment_actions, activity) -> Response:
|
def execute(self, message, attachment_actions, activity) -> Response: # pylint: disable=unused-argument
|
||||||
|
"""Execute the MakeMemeCommand and return an adaptive card."""
|
||||||
card_body: list = [
|
card_body: list = [
|
||||||
ColumnSet(
|
ColumnSet(
|
||||||
columns=[
|
columns=[
|
||||||
@ -45,13 +50,13 @@ class MakeMemeCommand(Command):
|
|||||||
size=FontSize.MEDIUM,
|
size=FontSize.MEDIUM,
|
||||||
),
|
),
|
||||||
TextBlock(
|
TextBlock(
|
||||||
"This bot uses memegen.link to generate memes. Click 'View Templates' to view available templates.",
|
"This bot uses memegen.link to generate memes. Click 'View Templates' to view available templates.", # pylint: disable=line-too-long
|
||||||
weight=FontWeight.LIGHTER,
|
weight=FontWeight.LIGHTER,
|
||||||
size=FontSize.SMALL,
|
size=FontSize.SMALL,
|
||||||
wrap=True,
|
wrap=True,
|
||||||
),
|
),
|
||||||
TextBlock(
|
TextBlock(
|
||||||
"Both fields are required. If you do not want to specify a value, please type a space.",
|
"Both fields are required. If you do not want to specify a value, please type a space.", # pylint: disable=line-too-long
|
||||||
weight=FontWeight.LIGHTER,
|
weight=FontWeight.LIGHTER,
|
||||||
size=FontSize.SMALL,
|
size=FontSize.SMALL,
|
||||||
wrap=True,
|
wrap=True,
|
||||||
@ -68,10 +73,7 @@ class MakeMemeCommand(Command):
|
|||||||
Choices(
|
Choices(
|
||||||
id="meme_type",
|
id="meme_type",
|
||||||
isMultiSelect=False,
|
isMultiSelect=False,
|
||||||
choices=[
|
choices=[Choice(title=x["name"], value=x["choiceval"]) for x in TEMPLATES],
|
||||||
Choice(title=x["name"], value=x["choiceval"])
|
|
||||||
for x in TEMPLATES
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
Text(id="text_top", placeholder="Top Text", maxLength=100),
|
Text(id="text_top", placeholder="Top Text", maxLength=100),
|
||||||
Text(
|
Text(
|
||||||
@ -103,6 +105,7 @@ class MakeMemeCallback(Command):
|
|||||||
"""Class to process user data and return meme."""
|
"""Class to process user data and return meme."""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
"""Initialize the MakeMemeCallback with command keyword and help message."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
card_callback_keyword="make_meme_callback_rbamzfyx",
|
card_callback_keyword="make_meme_callback_rbamzfyx",
|
||||||
delete_previous_message=True,
|
delete_previous_message=True,
|
||||||
@ -113,7 +116,8 @@ class MakeMemeCallback(Command):
|
|||||||
self.meme: str = ""
|
self.meme: str = ""
|
||||||
self.meme_filename: str = ""
|
self.meme_filename: str = ""
|
||||||
|
|
||||||
def pre_execute(self, message, attachment_actions, activity) -> str:
|
def pre_execute(self, message, attachment_actions, activity) -> str: # pylint: disable=unused-argument
|
||||||
|
"""Pre-execution logic for the MakeMemeCallback."""
|
||||||
self.meme: str = attachment_actions.inputs.get("meme_type")
|
self.meme: str = attachment_actions.inputs.get("meme_type")
|
||||||
self.text_top: str = attachment_actions.inputs.get("text_top")
|
self.text_top: str = attachment_actions.inputs.get("text_top")
|
||||||
self.text_bottom: str = attachment_actions.inputs.get("text_bottom")
|
self.text_bottom: str = attachment_actions.inputs.get("text_bottom")
|
||||||
@ -127,13 +131,12 @@ class MakeMemeCallback(Command):
|
|||||||
|
|
||||||
return "Generating your meme..."
|
return "Generating your meme..."
|
||||||
|
|
||||||
def execute(self, message, attachment_actions, activity) -> Response | None:
|
def execute(self, message, attachment_actions, activity) -> Response | None: # pylint: disable=unused-argument
|
||||||
|
"""Execute the MakeMemeCallback and return a response with the meme image."""
|
||||||
if self.error:
|
if self.error:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self.meme_filename: str = img.generate_api_url(
|
self.meme_filename: str = img.generate_api_url(self.meme, self.text_top, self.text_bottom)
|
||||||
self.meme, self.text_top, self.text_bottom
|
|
||||||
)
|
|
||||||
msg: Response = Response(
|
msg: Response = Response(
|
||||||
attributes={
|
attributes={
|
||||||
"roomId": activity["target"]["globalId"],
|
"roomId": activity["target"]["globalId"],
|
||||||
@ -143,5 +146,6 @@ class MakeMemeCallback(Command):
|
|||||||
)
|
)
|
||||||
return msg
|
return msg
|
||||||
|
|
||||||
def post_execute(self, message, attachment_actions, activity) -> None:
|
def post_execute(self, message, attachment_actions, activity) -> None: # pylint: disable=unused-argument
|
||||||
|
"""Post-execution logic for the MakeMemeCallback."""
|
||||||
return
|
return
|
||||||
|
@ -32,3 +32,6 @@ includes = []
|
|||||||
[build-system]
|
[build-system]
|
||||||
requires = ["pdm-backend"]
|
requires = ["pdm-backend"]
|
||||||
build-backend = "pdm.backend"
|
build-backend = "pdm.backend"
|
||||||
|
|
||||||
|
[tool.black]
|
||||||
|
line-length = 120
|
||||||
|
@ -2,19 +2,22 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
vars: dict = {
|
env_vars: dict = {
|
||||||
"APP_VERSION": "dev",
|
"APP_VERSION": "dev",
|
||||||
"WEBEX_API_KEY": "testing",
|
"WEBEX_API_KEY": "testing",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for var, value in vars.items():
|
for var, value in env_vars.items():
|
||||||
os.environ[var] = value
|
os.environ[var] = value
|
||||||
|
|
||||||
# needs to be imported AFTER environment variables are set
|
# needs to be imported AFTER environment variables are set
|
||||||
from app.config import config # pragma: no cover # noqa: E402
|
from app.config import (
|
||||||
|
config,
|
||||||
|
) # pylint: disable=wrong-import-position # pragma: no cover # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
def test_config() -> None:
|
def test_config() -> None:
|
||||||
assert config.webex_token == vars["WEBEX_API_KEY"]
|
"""Test the configuration settings."""
|
||||||
assert config.version == vars["APP_VERSION"]
|
assert config.webex_token == env_vars["WEBEX_API_KEY"]
|
||||||
|
assert config.version == env_vars["APP_VERSION"]
|
||||||
|
@ -29,8 +29,4 @@ def test_error_false() -> None:
|
|||||||
callback.text_top = "TEST"
|
callback.text_top = "TEST"
|
||||||
callback.text_bottom = "TEST"
|
callback.text_bottom = "TEST"
|
||||||
result: Response = callback.execute(None, None, {"target": {"globalId": "TEST"}})
|
result: Response = callback.execute(None, None, {"target": {"globalId": "TEST"}})
|
||||||
assert (
|
assert isinstance(result, Response) and result.roomId == "TEST" and result.files[0] == callback.meme_filename
|
||||||
isinstance(result, Response)
|
|
||||||
and result.roomId == "TEST"
|
|
||||||
and result.files[0] == callback.meme_filename
|
|
||||||
)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user