### Setup Outgoing Webhook Listener with Flask Source: https://github.com/bitcanon/synochat/blob/main/README.md Set up a Flask application to listen for outgoing webhooks from Synology Chat. This example handles authentication and responds with 'Pong'. ```python from flask import Flask, request from synochat.webhooks import OutgoingWebhook app = Flask(__name__) @app.route('/echo', methods=['POST']) def echo(): token = 'f69oQY4l5v7UVzKqmVfw1MQgFGZmxwODg1sndKIqsz8grAqYnKyerCRISQa1MiJj' webhook = OutgoingWebhook(request.form, token, verbose=True) if not webhook.authenticate(token): return webhook.createResponse('Outgoing Webhook authentication failed: Token mismatch.') print(webhook) return webhook.createResponse('Pong') if __name__ == '__main__': app.run('0.0.0.0', port=5001, debug = True) ``` -------------------------------- ### Install SynoChat Package Source: https://github.com/bitcanon/synochat/blob/main/README.md Install the synochat package using pip. ```bash $ pip install synochat ``` -------------------------------- ### Parameter Output Example Source: https://github.com/bitcanon/synochat/blob/main/README.md Expected console output when printing parameter objects. ```python : {'name': 'action', 'value': 'add', 'optional': False, 'detected': True} : {'name': 'code', 'value': '1234', 'optional': False, 'detected': True} : {'name': 'delay', 'value': '5', 'optional': True, 'detected': True} : {'name': 'silent', 'value': None, 'optional': True, 'detected': True} ``` -------------------------------- ### Implement Slash Command Receiver with Flask Source: https://github.com/bitcanon/synochat/blob/main/README.md A complete Flask application example demonstrating how to receive, authenticate, and parse slash command parameters. ```python from flask import Flask, request from synochat.webhooks import SlashCommand from synochat.exceptions import * app = Flask(__name__) @app.route('/slash', methods=['POST']) def slash(): token = 'LnTEXv9xKBwJtmIiXttGvpKaccEDHVJU5No4XX6oTnt7BQnPxbDwsWey1Pb9g9V2' command = SlashCommand(request.form) if not command.authenticate(token): return command.createResponse('Invalid token.') # Check if the command parameters are valid try: action = command.addParameter('action') code = command.addParameter('code', optional=False) delay = command.addParameter('delay', optional=True) silent = command.addParameter('silent', optional=True) except ParameterParseError: return command.createResponse('Slash command failed because one or more parameters are missing.') # Handle the first (positional) parameter if action.isPresent(): print(action) else: print(f"Parameter 'action' not detected in the command.") # Handle the second (positional) parameter if code.isPresent(): print(code) else: print(f"Parameter 'code' not detected in the command.") # Handle the third (optional) parameter if delay.isPresent(): print(delay) else: print(f"Parameter 'delay' not detected in the command.") # Handle the third (optional) parameter if silent.isPresent(): print(silent) else: print(f"Parameter 'silent' not detected in the command.") return command.createResponse('Slash command received.') if __name__ == '__main__': app.run('0.0.0.0', port=5001, debug = True) ``` -------------------------------- ### Define Slash Command Parameters Source: https://github.com/bitcanon/synochat/blob/main/README.md Example of a slash command string and the corresponding Python method calls to define optional parameters. ```bash /ping 1.1.1.1 count=4 time ``` ```python count = command.addParameter('count', optional=True) time = command.addParameter('time', optional=True) ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/bitcanon/synochat/blob/main/README.md Use virtualenv to create an isolated Python environment and activate it. ```bash $ virtualenv venv $ source venv/bin/activate ``` -------------------------------- ### Configure Incoming Webhook with Custom Settings Source: https://github.com/bitcanon/synochat/blob/main/README.md Customize webhook connection settings, including hostname, port, and SSL verification. Use this if your Synology NAS is not on the default port or lacks a valid SSL certificate. ```python webhook = IncomingWebhook('192.168.0.2', token, port=5001, verify_ssl=False) ``` -------------------------------- ### Configure Incoming Webhook to Use HTTP Source: https://github.com/bitcanon/synochat/blob/main/README.md Disable SSL verification and use HTTP instead of HTTPS for the webhook connection. This is generally not recommended for security reasons. ```python webhook.use_https = False ``` -------------------------------- ### Create Response with Attached File Source: https://github.com/bitcanon/synochat/blob/main/README.md Send a response message that includes a file of any type by providing its URL in the `file_url` parameter. ```python webhook.createResponse('Send text with a file attached.', file_url='http://ipv4.download.thinkbroadband.com/5MB.zip'') ``` -------------------------------- ### Create Response with Attached Image Source: https://github.com/bitcanon/synochat/blob/main/README.md Send a response message that includes an image by providing its URL in the `file_url` parameter. ```python webhook.createResponse('Send text with a photo attached.', file_url='https://www.synology.com/img/company/branding/synology_logo.jpg') ``` -------------------------------- ### Send Basic Message with Incoming Webhook Source: https://github.com/bitcanon/synochat/blob/main/README.md Send a simple text message to a Synology Chat channel using an IncomingWebhook object. Ensure the token is exactly 64 characters. ```python from synochat.webhooks import IncomingWebhook token = "w6Jw1Z6EpEONtabCfcTk6YObsaaj958fGzWOTQe0s33pl42RVLmkRUJBWoCgSfoz" webhook = IncomingWebhook('chat.example.com', token) webhook.send('This text is sent to Synology Chat.') ``` -------------------------------- ### Create Response with Custom Link Text Source: https://github.com/bitcanon/synochat/blob/main/README.md Format a response message to include a hyperlink with custom display text using the pipe symbol. ```python webhook.createResponse('Check out !') ``` -------------------------------- ### Access Class Properties Source: https://github.com/bitcanon/synochat/blob/main/README.md Read-only properties for accessing raw command text and individual parameter details. ```python command.text ``` ```python parameter.name parameter.value parameter.optional parameter.detected ``` -------------------------------- ### Send Messages with IncomingWebhook Source: https://context7.com/bitcanon/synochat/llms.txt Use IncomingWebhook to post messages to Synology Chat channels. Supports plain text, links, images, and file attachments. Handles rate limiting automatically. Configure custom ports, SSL, and send delays. ```python from synochat.webhooks import IncomingWebhook from synochat.exceptions import InvalidTokenError, RateLimitError, InvalidPayloadError # Initialize the webhook with hostname and token token = "w6Jw1Z6EpEONtabCfcTk6YObsaaj958fGzWOTQe0s33pl42RVLmkRUJBWoCgSfoz" webhook = IncomingWebhook('chat.example.com', token) # Send a simple text message try: webhook.send('Hello from SynoChat!') except InvalidTokenError: print("Authentication failed: Invalid token") # Send message with embedded link webhook.send('Check out !') # Send message with an attached image webhook.send('New server alert!', file_url='https://example.com/alert-image.png') # Send message with a file attachment webhook.send('Daily report attached', file_url='https://example.com/report.pdf') # Advanced configuration for custom port and SSL settings webhook_custom = IncomingWebhook( '192.168.1.100', token, port=5001, verify_ssl=False, send_delay_enabled=True, send_delay=0.75 ) # Disable HTTPS if needed webhook_custom.use_https = False # Send multiple messages with rate limiting handled automatically messages = ['Alert 1', 'Alert 2', 'Alert 3'] for msg in messages: try: webhook_custom.send(msg) except RateLimitError: print("Rate limit exceeded, waiting...") ``` -------------------------------- ### Handle Custom Slash Commands with Parameters Source: https://context7.com/bitcanon/synochat/llms.txt Processes slash commands with positional and optional parameters. Responses are only visible to the user who issued the command. Ensure correct token for authentication. ```python from flask import Flask, request from synochat.webhooks import SlashCommand from synochat.exceptions import ParameterParseError app = Flask(__name__) @app.route('/command', methods=['POST']) def handle_command(): token = 'LnTEXv9xKBwJtmIiXttGvpKaccEDHVJU5No4XX6oTnt7BQnPxbDwsWey1Pb9g9V2' command = SlashCommand(request.form, verbose=True) # Authenticate the request if not command.authenticate(token): return command.createResponse('Authentication failed.') # Example command: /deploy production v1.2.3 rollback notify=slack try: # Positional parameters (required, must be in order) environment = command.addParameter('environment') # First positional version = command.addParameter('version') # Second positional # Optional parameters (can be in any order after positional params) rollback = command.addParameter('rollback', optional=True) notify = command.addParameter('notify', optional=True) except ParameterParseError: return command.createResponse('Missing required parameters. Usage: /deploy [rollback] [notify=channel]') # Check if parameters are present and get their values response_parts = [] if environment.isPresent(): response_parts.append(f"Environment: {environment.value}") if version.isPresent(): response_parts.append(f"Version: {version.value}") if rollback.isPresent(): response_parts.append("Rollback: Enabled") if notify.isPresent() and notify.value: response_parts.append(f"Notify: {notify.value}") # Access raw command text print(f"Full command: {command.text}") return command.createResponse('\n'.join(response_parts)) if __name__ == '__main__': app.run('0.0.0.0', port=5001, debug=True) # Parameter object properties: # parameter.name - Parameter name as defined # parameter.value - Parameter value (None if flag-style optional) # parameter.optional - Boolean indicating if optional # parameter.detected - Boolean indicating if parameter was found # parameter.isPresent() - Same as detected, more readable # Example slash command formats: # /deploy production v1.2.3 -> Two positional params # /deploy production v1.2.3 rollback -> With optional flag # /deploy production v1.2.3 notify=slack -> With optional key=value # /deploy production v1.2.3 rollback notify=email -> All parameters ``` -------------------------------- ### Handle Trigger Words with OutgoingWebhook Source: https://context7.com/bitcanon/synochat/llms.txt Use OutgoingWebhook to process messages from Synology Chat based on trigger words. Authenticates requests and provides access to message metadata. Allows sending formatted responses, including links and images. ```python from flask import Flask, request from synochat.webhooks import OutgoingWebhook app = Flask(__name__) @app.route('/webhook', methods=['POST']) def handle_webhook(): # Your webhook token from Synology Chat integration settings token = 'f69oQY4l5v7UVzKqmVfw1MQgFGZmxwODg1sndKIqsz8grAqYnKyerCRISQa1MiJj' # Create webhook instance from incoming request data webhook = OutgoingWebhook(request.form, token, verbose=True) # Authenticate the request if not webhook.authenticate(token): return webhook.createResponse('Authentication failed: Token mismatch.') # Access webhook properties print(f"Channel: {webhook.channel_name}") print(f"User: {webhook.username}") print(f"Message: {webhook.text}") print(f"Trigger word: {webhook.trigger_word}") print(f"Timestamp: {webhook.timestamp}") # Respond based on trigger word if webhook.trigger_word.lower() == 'ping': return webhook.createResponse('Pong!') # Response with embedded link if webhook.trigger_word.lower() == 'help': return webhook.createResponse('Visit our docs: ') # Response with attached image if webhook.trigger_word.lower() == 'status': return webhook.createResponse( 'Current system status:', file_url='https://example.com/status-dashboard.png' ) return webhook.createResponse(f'Received: {webhook.text}') if __name__ == '__main__': app.run('0.0.0.0', port=5001, debug=True) # Available OutgoingWebhook properties: # webhook.client_token - Token provided by your application # webhook.server_token - Token from Synology Chat server # webhook.channel_id - Numeric channel identifier # webhook.channel_type - Channel type (1 = standard) # webhook.channel_name - Human-readable channel name # webhook.user_id - Numeric user identifier # webhook.username - Username who sent the message # webhook.post_id - Unique message identifier # webhook.thread_id - Thread identifier (0 if not in thread) ``` -------------------------------- ### Send Message with Attached File Source: https://github.com/bitcanon/synochat/blob/main/README.md Attach a file to the message by providing its URL to the 'file_url' parameter. This can be any file type. ```python webhook.send('Send text with a file attached.', file_url='http://ipv4.download.thinkbroadband.com/5MB.zip') ``` -------------------------------- ### Send Message with Attached Image Source: https://github.com/bitcanon/synochat/blob/main/README.md Include an image in the message by providing its URL to the 'file_url' parameter. ```python webhook.send('Send text with a photo attached.', file_url='https://www.synology.com/img/company/branding/synology_logo.jpg') ``` -------------------------------- ### Handle Synology Chat API Exceptions Source: https://context7.com/bitcanon/synochat/llms.txt Provides specific exception classes for granular error handling of Synology Chat API interactions. Use these to manage issues like invalid tokens, rate limits, or payload errors. ```python from synochat.webhooks import IncomingWebhook from synochat.exceptions import ( InvalidTokenError, InvalidApiError, InvalidMethodError, InvalidVersionError, InvalidPayloadError, RateLimitError, UnknownApiError, ParameterParseError ) webhook = IncomingWebhook('chat.example.com', 'your-token-here') try: webhook.send('Test message') except InvalidTokenError as e: print(f"Token error: {e.message}") # Handle: Verify token in Synology Chat integration settings except InvalidPayloadError as e: print(f"Payload error: {e.message}") # Handle: Check message format and file_url validity except RateLimitError as e: print(f"Rate limit: {e.message}") # Handle: Increase send_delay or add retry logic except UnknownApiError as e: print(f"Unknown error: {e.message}") # Handle: Log for debugging, check Synology Chat logs ``` -------------------------------- ### Debug Outgoing Webhook Request Data Source: https://github.com/bitcanon/synochat/blob/main/README.md Print the webhook object to inspect the data received from Synology Chat for debugging purposes. ```bash : {'client_token': 'f69oQY4l5v7UVzKqmVfw1MQgFGZmxwODg1sndKIqsz8grAqYnKyerCRISQa1MiJj', 'server_token': 'f69oQY4l5v7UVzKqmVfw1MQgFGZmxwODg1sndKIqsz8grAqYnKyerCRISQa1MiJj', 'channel_id': '34', 'channel_type': '1', 'channel_name': 'Labb', 'user_id': '4', 'username': 'mikael', 'post_id': '146028888230', 'thread_id': '0', 'timestamp': '1647060330657', 'text': 'Ping', 'trigger_word': 'Ping', 'verbose': True} ``` -------------------------------- ### Configure Send Delay for Incoming Webhooks Source: https://github.com/bitcanon/synochat/blob/main/README.md Disable the default send delay or adjust its duration for incoming webhooks. The default delay is 0.5 seconds. ```python webhook.send_delay_enabled = False webhook.send_delay = 0.75 ``` ```python webhook = IncomingWebhook('192.168.0.2', token, port=5001, verify_ssl=False, send_delay_enabled=False, send_delay=1.5) ``` -------------------------------- ### Create Response with Embedded Link Source: https://github.com/bitcanon/synochat/blob/main/README.md Format a response message to include a hyperlink using angle brackets. ```python webhook.createResponse('Send text with a link embedded ') ``` -------------------------------- ### Send Message with Formatted Link Text Source: https://github.com/bitcanon/synochat/blob/main/README.md Define custom display text for a hyperlink by appending '|text' after the URL within the link tag. ```python webhook.send('Check out !') ``` -------------------------------- ### Set Incoming Webhook Properties Directly Source: https://github.com/bitcanon/synochat/blob/main/README.md Modify webhook connection parameters by directly assigning values to the webhook object's properties before sending a message. ```python webhook.hostname = "nas.yourdomain.com" webhook.port = "443" webhook.use_https = True webhook.verify_ssl = True webhook.token = "..." ``` -------------------------------- ### Define Positional Parameter for Slash Command Source: https://github.com/bitcanon/synochat/blob/main/README.md Add a positional parameter to a slash command. Positional parameters are required and must appear first in the command. ```python ip = command.addParameter('ip') ``` -------------------------------- ### Access Outgoing Webhook Properties Source: https://github.com/bitcanon/synochat/blob/main/README.md Read-only properties to access data fields from an outgoing webhook request. ```python webhook.client_token webhook.server_token webhook.channel_id webhook.channel_type webhook.channel_name webhook.user_id webhook.username webhook.post_id webhook.thread_id webhook.timestamp webhook.text webhook.trigger_word webhook.verbose ``` -------------------------------- ### Send Message with Embedded Link Source: https://github.com/bitcanon/synochat/blob/main/README.md Embed a hyperlink within the message text. The link is enclosed in angle brackets. ```python webhook.send('Send text with a link embedded ') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.