Example #1
0
func GetSMSIntlBalance() (int, error) {
	c := config.Get()

	if c.SMSOn == false {
		return 0, nil
	}

	resp, err := http.Get(fmt.Sprintf("%s/user/get_credits/1/1.1?username=%s&password=%s",
		c.SMSIntlServer,
		c.SMSIntlUser,
		c.SMSIntlPasswd))

	if err != nil {
		log.Println("HTTP Get Error", err.Error())
		return 0, err
	}

	// read the response
	body, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	if err != nil {
		log.Println(err.Error())
		return 0, err
	}
	print("got response of", body)
	return 1, nil
}
Example #2
0
func GetBalance() (int, error) {
	c := config.Get()

	if c.SMSOn == false {
		return 0, nil
	}

	resp, err := http.PostForm(
		c.SMSServer,
		url.Values{
			"username": {c.SMSUser},
			"password": {c.SMSPasswd},
			"action":   {"balance"},
		})

	if err != nil {
		log.Println("HTTP Post Error", err.Error())
		return 0, err
	}

	/*	log.Println(resp)
		log.Println("status", resp.Status)
		log.Println("status code", resp.StatusCode)
		log.Println("proto", resp.Proto)
		log.Println("major", resp.ProtoMajor)
		log.Println("minor", resp.ProtoMinor)
		log.Println("header", resp.Header)
		log.Println("content length", resp.ContentLength)
		log.Println("transfer", resp.TransferEncoding)
		log.Println("trailer", resp.Trailer)
		log.Println("close", resp.Close)
		log.Println("req", resp.Request)
		log.Println("tls", resp.TLS)
		log.Println("body", resp.Body)
	*/

	// read the response
	body, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	if err != nil {
		log.Println(err.Error())
		return 0, err
	}
	isok := string(body[:3])
	if isok != "OK:" {
		return 0, errors.New("Status Code is" + isok)
	}
	s := string(body[3:])
	return strconv.Atoi(s)
}
Example #3
0
func mailerDaemon() {

	c := config.Get()
	log.Println("starting mailer with", c.MailServer, c.MailPort, c.MailUser, c.MailPasswd)
	d := gomail.NewPlainDialer(
		c.MailServer,
		c.MailPort,
		c.MailUser,
		c.MailPasswd)

	d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

	var s gomail.SendCloser
	var err error
	open := false
	for {
		select {
		case m, ok := <-MailChannel:
			if !ok {
				return
			}
			if !open {
				if s, err = d.Dial(); err != nil {
					panic(err)
				}
				open = true
			}
			if err := gomail.Send(s, m); err != nil {
				log.Print(err)
			}
		// Close the connection to the SMTP server if no email was sent in
		// the last couple of minutes.
		case <-time.After(120 * time.Second):
			if open {
				if err := s.Close(); err != nil {
					panic(err)
				}
				open = false
			}
		}
	}
}
Example #4
0
func Send(number string, message string, ref string) error {

	c := config.Get()
	resp, err := http.PostForm(
		c.SMSServer,
		url.Values{
			"username": {c.SMSUser},
			"password": {c.SMSPasswd},
			"to":       {number},
			"from":     {"SBS Intl"},
			"ref":      {ref},
			"message":  {message},
		})

	if err != nil {
		log.Println("HTTP Post Error", err.Error())
		return err
	}

	// read the response
	body, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	lines := strings.Split(string(body), "\n")
	for _, v := range lines {
		p := strings.Split(v, ":")
		switch p[0] {
		case "OK":
			log.Println("SMS OK", p[1], "ref", p[2])
		case "BAD":
			log.Println("SMS BAD", p[1], "reason", p[2])
			return errors.New(p[2])
		case "ERROR":
			log.Println("SMS ERROR", p[1])
			return errors.New(p[1])
		}
	}
	return nil
}