Skip to content

Bug: The param variable of Alibaba Cloud PNVS SMS provider is missing the key 'min' #5173

@Sunny-Law

Description

@Sunny-Law

PNVS SendSmsVerifyCode API: https://help.aliyun.com/zh/pnvs/developer-reference/api-dypnsapi-2017-05-25-sendsmsverifycode?spm=a2c4g.11186623.0.0.718ea78eiVGQuN

The PNVS SendSmsVerifyCode API requires the parameter templateParam to pass a JSON string containing the keys code and min, but the code is missing the min key, causing the SMS verification code to fail to send and return the error "Invalid content for template variable min."

# file: object/sms.go
func SendSms(provider *Provider, content string, phoneNumbers ...string) error {
	client, err := getSmsClient(provider)
	if err != nil {
		return err
	}

	if provider.Type == sender.Twilio {
		if provider.AppId != "" {
			phoneNumbers = append([]string{provider.AppId}, phoneNumbers...)
		}
	} else if provider.Type == sender.Aliyun || provider.Type == "Alibaba Cloud PNVS SMS" {
		for i, number := range phoneNumbers {
			phoneNumbers[i] = strings.TrimPrefix(number, "+86")
		}
	}

	params := map[string]string{}
	if provider.Type == sender.TencentCloud {
		params["0"] = content
	} else {
		params["code"] = content
	}

	# When the provider's type is 'Alibaba Cloud PNVS SMS', call the SendMessage method in sms_pnvs.go
	err = client.SendMessage(params, phoneNumbers...)
	return err
}
# file: object/sms_pnvs.go
func (c *PnvsSmsClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
	if len(targetPhoneNumber) == 0 {
		return fmt.Errorf("missing parameter: targetPhoneNumber")
	}

	// PNVS sends to one phone number at a time
	phoneNumber := targetPhoneNumber[0]

	request := dypnsapi.CreateSendSmsVerifyCodeRequest()
	request.Scheme = "https"
	request.PhoneNumber = phoneNumber
	request.TemplateCode = c.template
	request.SignName = c.sign

	// TemplateParam is optional for PNVS as it can auto-generate verification codes
	// But if params are provided, we'll pass them
	if len(param) > 0 {
		templateParam, err := json.Marshal(param)
		if err != nil {
			return err
		}
		request.TemplateParam = string(templateParam)
	}

	response, err := c.core.SendSmsVerifyCode(request)
	if err != nil {
		return err
	}

	if response.Code != "OK" {
		if response.Message != "" {
			return fmt.Errorf(response.Message)
		}
		return fmt.Errorf("PNVS SMS send failed with code: %s", response.Code)
	}

	return nil
}
Image

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingreleased

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions