|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from typing import Tuple |
| 4 | + |
| 5 | +project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../..')) |
| 6 | +sys.path.append(project_root) |
| 7 | + |
| 8 | +from plugins.telegram.telegram_plugin_gamesdk.telegram_plugin import TelegramPlugin |
| 9 | +from src.game_sdk.game.custom_types import Function, FunctionResultStatus, Argument |
| 10 | + |
| 11 | + |
| 12 | +def send_message_executable(tg_plugin: TelegramPlugin, chat_id: str, text: str) -> Tuple[FunctionResultStatus, str, dict]: |
| 13 | + try: |
| 14 | + tg_plugin.send_message(chat_id=chat_id, text=text) |
| 15 | + return FunctionResultStatus.DONE, "Message sent successfully", {} |
| 16 | + except Exception as e: |
| 17 | + return FunctionResultStatus.FAILED, str(e), {} |
| 18 | + |
| 19 | +def send_message_fn(bot: TelegramPlugin) -> Function: |
| 20 | + return Function( |
| 21 | + fn_name="send_message", |
| 22 | + fn_description="Send a text message to a Telegram chat", |
| 23 | + args=[ |
| 24 | + Argument(name="chat_id", description="ID of the chat to send the message to", type="str"), |
| 25 | + Argument(name="text", description="Text message to send", type="str"), |
| 26 | + ], |
| 27 | + executable=lambda chat_id, text: send_message_executable(bot, chat_id, text), |
| 28 | + ) |
| 29 | + |
| 30 | +def send_media_executable(tg_plugin: TelegramPlugin, chat_id: str, media_type: str, media: str, caption: str = None) -> Tuple[FunctionResultStatus, str, dict]: |
| 31 | + try: |
| 32 | + tg_plugin.send_media(chat_id=chat_id, media_type=media_type, media=media, caption=caption) |
| 33 | + return FunctionResultStatus.DONE, "Media sent successfully", {} |
| 34 | + except Exception as e: |
| 35 | + return FunctionResultStatus.FAILED, str(e), {} |
| 36 | + |
| 37 | +def send_media_fn(bot: TelegramPlugin) -> Function: |
| 38 | + return Function( |
| 39 | + fn_name="send_media", |
| 40 | + fn_description="Send a media message to a Telegram chat", |
| 41 | + args=[ |
| 42 | + Argument(name="chat_id", description="ID of the chat to send the message to", type="str"), |
| 43 | + Argument(name="media_type", description="Type of media to send (photo, document, video, audio)", type="str"), |
| 44 | + Argument(name="media", description="Media URL or file path to send", type="str"), |
| 45 | + Argument(name="caption", description="Optional caption for the media", type="str", optional=True), |
| 46 | + ], |
| 47 | + executable=lambda chat_id, media_type, media, caption=None: send_media_executable(bot, chat_id, media_type, media, caption), |
| 48 | + ) |
| 49 | + |
| 50 | +def create_poll_executable(tg_plugin: TelegramPlugin, chat_id: str, question: str, options: list[str], is_anonymous: bool = True, allows_multiple_answers: bool = False) -> Tuple[FunctionResultStatus, str, dict]: |
| 51 | + try: |
| 52 | + tg_plugin.create_poll(chat_id=chat_id, question=question, options=options, is_anonymous=is_anonymous, allows_multiple_answers=allows_multiple_answers) |
| 53 | + return FunctionResultStatus.DONE, "Poll created successfully", {} |
| 54 | + except Exception as e: |
| 55 | + return FunctionResultStatus.FAILED, str(e), {} |
| 56 | + |
| 57 | +def create_poll_fn(bot: TelegramPlugin) -> Function: |
| 58 | + return Function( |
| 59 | + fn_name="create_poll", |
| 60 | + fn_description="Create a poll in a Telegram chat", |
| 61 | + args=[ |
| 62 | + Argument(name="chat_id", description="ID of the chat to create the poll in", type="str"), |
| 63 | + Argument(name="question", description="Question to ask in the poll", type="str"), |
| 64 | + Argument(name="options", description="List of options for the poll", type="List[str]"), |
| 65 | + Argument(name="is_anonymous", description="Whether the poll is anonymous (default: True)", type="bool", optional=True), |
| 66 | + Argument(name="allows_multiple_answers", description="Whether multiple answers are allowed (default: False)", type="bool", optional=True), |
| 67 | + ], |
| 68 | + executable=lambda chat_id, question, options, is_anonymous=False, allows_multiple_answers=False: create_poll_executable(bot, chat_id, question, options, is_anonymous, allows_multiple_answers), |
| 69 | + ) |
| 70 | + |
| 71 | +def pin_message_executable(tg_plugin: TelegramPlugin, chat_id: str, message_id: int) -> Tuple[FunctionResultStatus, str, dict]: |
| 72 | + try: |
| 73 | + tg_plugin.pin_message(chat_id=chat_id, message_id=message_id) |
| 74 | + return FunctionResultStatus.DONE, "Message pinned successfully", {} |
| 75 | + except Exception as e: |
| 76 | + return FunctionResultStatus.FAILED, str(e), {} |
| 77 | + |
| 78 | +def pin_message_fn(bot: TelegramPlugin) -> Function: |
| 79 | + return Function( |
| 80 | + fn_name="pin_message", |
| 81 | + fn_description="Pin a message in a Telegram chat", |
| 82 | + args=[ |
| 83 | + Argument(name="chat_id", description="ID of the chat to pin the message in", type="str"), |
| 84 | + Argument(name="message_id", description="ID of the message to pin", type="int"), |
| 85 | + ], |
| 86 | + executable=lambda chat_id, message_id: pin_message_executable(bot, chat_id, message_id), |
| 87 | + ) |
| 88 | + |
| 89 | +def unpin_message_executable(tg_plugin: TelegramPlugin, chat_id: str, message_id: int) -> Tuple[FunctionResultStatus, str, dict]: |
| 90 | + try: |
| 91 | + tg_plugin.unpin_message(chat_id=chat_id, message_id=message_id) |
| 92 | + return FunctionResultStatus.DONE, "Message unpinned successfully", {} |
| 93 | + except Exception as e: |
| 94 | + return FunctionResultStatus.FAILED, str(e), {} |
| 95 | + |
| 96 | +def unpin_message_fn(bot: TelegramPlugin) -> Function: |
| 97 | + return Function( |
| 98 | + fn_name="unpin_message", |
| 99 | + fn_description="Unpin a message in a Telegram chat", |
| 100 | + args=[ |
| 101 | + Argument(name="chat_id", description="ID of the chat to unpin the message in", type="str"), |
| 102 | + Argument(name="message_id", description="ID of the message to unpin", type="int"), |
| 103 | + ], |
| 104 | + executable=lambda chat_id, message_id: unpin_message_executable(bot, chat_id, message_id), |
| 105 | + ) |
| 106 | + |
| 107 | +def delete_message_executable(tg_plugin: TelegramPlugin, chat_id: str, message_id: int) -> Tuple[FunctionResultStatus, str, dict]: |
| 108 | + try: |
| 109 | + tg_plugin.delete_message(chat_id=chat_id, message_id=message_id) |
| 110 | + return FunctionResultStatus.DONE, "Message deleted successfully", {} |
| 111 | + except Exception as e: |
| 112 | + return FunctionResultStatus.FAILED, str(e), {} |
| 113 | + |
| 114 | +def delete_message_fn(bot: TelegramPlugin) -> Function: |
| 115 | + return Function( |
| 116 | + fn_name="delete_message", |
| 117 | + fn_description="Delete a message in a Telegram chat", |
| 118 | + args=[ |
| 119 | + Argument(name="chat_id", description="ID of the chat to delete the message in", type="str"), |
| 120 | + Argument(name="message_id", description="ID of the message to delete", type="int"), |
| 121 | + ], |
| 122 | + executable=lambda chat_id, message_id: delete_message_executable(bot, chat_id, message_id), |
| 123 | + ) |
0 commit comments