Skip to content

Commit

Permalink
feat(): support phone call
Browse files Browse the repository at this point in the history
  • Loading branch information
EZ4BRUCE committed Mar 8, 2024
1 parent aa95eb6 commit 7cd7341
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
6 changes: 0 additions & 6 deletions engine/apps/mocloud/mocloud_phone_provider.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import logging
from random import randint


import os
import sys



from django.core.cache import cache

from ..phone_notifications.exceptions import FailedToSendSMS, FailedToStartVerification
Expand Down
4 changes: 2 additions & 2 deletions engine/apps/mocloud/mocloud_sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def create_client(
)
return Dysmsapi20170525Client(config)

def send_sms_notification(self, number, parmas):
def send_sms_notification(self, number, parmas):
send_sms_request = dysmsapi_20170525_models.SendSmsRequest(
phone_numbers=number,
sign_name=DEFAULT_SIGN_NAME,
Expand Down Expand Up @@ -67,7 +67,7 @@ def send_sms_verification(self, number, code):
)
runtime = util_models.RuntimeOptions()
try:
self.sms_client.send_sms_with_options(send_sms_request, runtime)
resp=self.sms_client.send_sms_with_options(send_sms_request, runtime)
logger.info(f"send verification code [{code}] to [{number}] success")
except Exception as error:
logger.error(f"send verification code [{code}] to [{number}] failed")
Expand Down
4 changes: 2 additions & 2 deletions engine/apps/mocloud/mocloud_vms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def send_vms_notification(self, number, parmas):
)
try:
# 复制代码运行请自行打印 API 的返回值
self.vms_client.single_call_by_voice_with_options(
self.vms_client.single_call_by_tts_with_options(
single_call_by_voice_request, util_models.RuntimeOptions())
except Exception as error:
# 错误 message
print(error.message)
# print(error.message)
# 诊断地址
print(error.data.get("Recommend"))
UtilClient.assert_as_string(error.message)
27 changes: 18 additions & 9 deletions engine/apps/phone_notifications/phone_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
from common.api_helpers.utils import create_engine_url
from common.utils import clean_markup
from settings.base import PHONE_PROVIDER

import json
from apps.labels.utils import get_alert_group_labels_dict, get_labels_dict, is_labels_feature_enabled

# from apps.mocloud.mocloud_template import VMSTemplate

from .exceptions import (
Expand Down Expand Up @@ -105,15 +106,15 @@ def _notify_by_provider_call(self, user, message, alert_group) -> Optional[Provi
raise CallsLimitExceeded
elif calls_left < 3:
message = self._add_call_limit_warning(calls_left, message)

if PHONE_PROVIDER == "mocloud":
parmas = {"cluster_env": alert_group.channel.short_name,
"alert_count": alert_group.alerts.count(), }
parmasStr=json.dumps(parmas)
"alert_count": alert_group.alerts.count(), }
parmasStr = json.dumps(parmas)
# in mocloud alert, message is actually template parmas (json)
# parmasStr = VMSTemplate.rander_params(alert_group)
return self.phone_provider.make_notification_call(user.verified_phone_number, parmasStr)

return self.phone_provider.make_notification_call(user.verified_phone_number, message)

def _notify_by_cloud_call(self, user, message):
Expand Down Expand Up @@ -165,7 +166,6 @@ def notify_by_sms(self, user, alert_group, notification_policy):
It handles business logic - limits, cloud notifications and UserNotificationPolicyLogRecord creation
SMS itself is handled by phone provider.
"""

from apps.base.models import UserNotificationPolicyLogRecord

log_record_error_code = None
Expand All @@ -185,7 +185,8 @@ def notify_by_sms(self, user, alert_group, notification_policy):
self._notify_by_cloud_sms(user, message)
record.save()
else:
provider_sms = self._notify_by_provider_sms(user, message)
provider_sms = self._notify_by_provider_sms(
user, message, alert_group)
record.save()
if provider_sms:
provider_sms.link_and_save(record)
Expand Down Expand Up @@ -214,18 +215,26 @@ def notify_by_sms(self, user, alert_group, notification_policy):
user_notification_action_triggered_signal.send(
sender=PhoneBackend.notify_by_sms, log_record=log_record)

def _notify_by_provider_sms(self, user, message) -> Optional[ProviderSMS]:
def _notify_by_provider_sms(self, user, message, alert_group) -> Optional[ProviderSMS]:
"""
_notify_by_provider_sms sends a notification sms using configured phone provider.
"""
if not self._validate_user_number(user):
raise NumberNotVerified

# labels = get_alert_group_labels_dict(alert_group)
sms_left = self._validate_sms_left(user)
if sms_left <= 0:
raise SMSLimitExceeded
elif sms_left < 3:
message = self._add_sms_limit_warning(sms_left, message)

if PHONE_PROVIDER == "mocloud":
# in mocloud alert, message is actually template parmas (json)
parmasStr = '{"cluster_env": "%s","alert_time": "%s","alert_name": "%s"}' % (
alert_group.channel.short_name, alert_group.started_at, alert_group.web_title_cache)
# parmasStr = VMSTemplate.rander_params(alert_group)
return self.phone_provider.send_notification_sms(user.verified_phone_number, parmasStr)

return self.phone_provider.send_notification_sms(user.verified_phone_number, message)

def _notify_by_cloud_sms(self, user, message):
Expand Down
3 changes: 3 additions & 0 deletions engine/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,5 +472,8 @@ x-wr-timezone==0.0.6
zipp==3.17.0
# via importlib-metadata

alibabacloud_dysmsapi20170525==2.0.24
alibabacloud_dyvmsapi20170525==3.0.0

# The following packages are considered to be unsafe in a requirements file:
# setuptools
2 changes: 1 addition & 1 deletion engine/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ class BrokerTypes:

# map of phone provider alias to importpath.
# Used in get_phone_provider function to dynamically load current provider.
DEFAULT_PHONE_PROVIDER = "twilio"
DEFAULT_PHONE_PROVIDER = "mocloud"
PHONE_PROVIDERS = {
"twilio": "apps.twilioapp.phone_provider.TwilioPhoneProvider",
"simple": "apps.phone_notifications.simple_phone_provider.SimplePhoneProvider",
Expand Down

0 comments on commit 7cd7341

Please sign in to comment.