Implement Discord and Slack notifications for call events

Settings & Configuration:
- Added Discord webhook URL, Slack webhook URL settings in admin
- Added notification type toggles (incoming calls, queue timeouts, missed calls)
- Added queue timeout threshold setting (30-1800 seconds)
- Registered all new settings with WordPress options system

Notification System:
- Created TWP_Notifications class for Discord/Slack webhook handling
- Rich message formatting with embeds/attachments for both platforms
- Color-coded notifications (blue=incoming, yellow=timeout, red=missed)
- Comprehensive error handling and logging

Integration Points:
- Incoming calls: Notifications sent when calls enter queues
- Queue timeouts: Automated monitoring via cron job (every minute)
- Missed calls: Notifications for browser phone and general missed calls
- Added notified_timeout column to prevent duplicate timeout notifications

Features:
- Professional Discord embeds with fields and timestamps
- Slack attachments with proper formatting and colors
- Automatic cron job setup for queue timeout monitoring
- Fallback to SMS notifications while Discord/Slack also work
- Configurable notification types and timeout thresholds

This provides real-time call notifications to Discord channels and Slack channels,
helping teams stay informed about incoming calls and queue issues even when
SMS notifications aren't working due to validation delays.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-08-13 10:47:59 -07:00
parent 97a064bf83
commit 534d343526
6 changed files with 444 additions and 0 deletions

View File

@@ -481,6 +481,60 @@ class TWP_Admin {
<p class="description">Default Twilio phone number to use as sender for SMS messages when not in a workflow context.</p>
</td>
</tr>
<!-- Discord/Slack Notifications Section -->
<tr valign="top">
<td colspan="2">
<h3 style="margin-top: 30px; margin-bottom: 15px;">Discord & Slack Notifications</h3>
<p class="description">Configure webhook URLs to receive call notifications in Discord and/or Slack channels.</p>
</td>
</tr>
<tr valign="top">
<th scope="row">Discord Webhook URL</th>
<td>
<input type="url" name="twp_discord_webhook_url" value="<?php echo esc_attr(get_option('twp_discord_webhook_url')); ?>" class="regular-text" placeholder="https://discord.com/api/webhooks/..." />
<p class="description">Discord webhook URL for call notifications. <a href="https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks" target="_blank">How to create a Discord webhook</a></p>
</td>
</tr>
<tr valign="top">
<th scope="row">Slack Webhook URL</th>
<td>
<input type="url" name="twp_slack_webhook_url" value="<?php echo esc_attr(get_option('twp_slack_webhook_url')); ?>" class="regular-text" placeholder="https://hooks.slack.com/services/..." />
<p class="description">Slack webhook URL for call notifications. <a href="https://slack.com/help/articles/115005265063-Incoming-webhooks-for-Slack" target="_blank">How to create a Slack webhook</a></p>
</td>
</tr>
<tr valign="top">
<th scope="row">Notification Settings</th>
<td>
<fieldset>
<label>
<input type="checkbox" name="twp_notify_on_incoming_calls" value="1" <?php checked(get_option('twp_notify_on_incoming_calls', 1)); ?> />
Notify on incoming calls
</label><br>
<label>
<input type="checkbox" name="twp_notify_on_queue_timeout" value="1" <?php checked(get_option('twp_notify_on_queue_timeout', 1)); ?> />
Notify when calls stay in queue too long
</label><br>
<label>
<input type="checkbox" name="twp_notify_on_missed_calls" value="1" <?php checked(get_option('twp_notify_on_missed_calls', 1)); ?> />
Notify on missed calls
</label>
</fieldset>
<p class="description">Choose which events trigger Discord/Slack notifications.</p>
</td>
</tr>
<tr valign="top">
<th scope="row">Queue Timeout Threshold</th>
<td>
<input type="number" name="twp_queue_timeout_threshold" value="<?php echo esc_attr(get_option('twp_queue_timeout_threshold', 300)); ?>" min="30" max="1800" />
<span>seconds</span>
<p class="description">Send notification if call stays in queue longer than this time (30-1800 seconds).</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
@@ -2725,6 +2779,14 @@ class TWP_Admin {
register_setting('twilio-wp-settings-group', 'twp_urgent_keywords');
register_setting('twilio-wp-settings-group', 'twp_sms_notification_number');
register_setting('twilio-wp-settings-group', 'twp_default_sms_number');
// Discord/Slack notification settings
register_setting('twilio-wp-settings-group', 'twp_discord_webhook_url');
register_setting('twilio-wp-settings-group', 'twp_slack_webhook_url');
register_setting('twilio-wp-settings-group', 'twp_notify_on_incoming_calls');
register_setting('twilio-wp-settings-group', 'twp_notify_on_queue_timeout');
register_setting('twilio-wp-settings-group', 'twp_notify_on_missed_calls');
register_setting('twilio-wp-settings-group', 'twp_queue_timeout_threshold');
}
/**