-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (49 loc) · 1.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"github.com/streadway/amqp"
"isms/sms"
"isms/sms/util"
"log"
"os"
)
func main() {
// Load environment variables.
util.LoadEnv()
connection, err := amqp.Dial(os.Getenv("CLOUD_AMQP_URL"))
defer connection.Close()
if err != nil {
util.FailResponse(err, "Cannot connect to CloudAMQP service provider")
}
channel, err := connection.Channel()
defer channel.Close();
if err != nil {
util.FailResponse(err, "Failed to initialed channel")
}
queueName := "isms"
q, err := channel.QueueDeclare(queueName, false, false, false, false, nil)
if err != nil {
util.FailResponse(err, "Failed to declare a queue")
}
messages, err := channel.Consume(q.Name, "", false, false, false, false, nil)
if err != nil {
util.FailResponse(err, "Queue failed to bind to consumer")
}
// Declare boolean channel
forever := make(chan bool)
go func() {
for response := range messages {
// Parse Json String into struct for easy to access properties.
contact := util.ParseResponse(response.Body)
switch contact.Type {
case "send_sms":
sms.SendDirectSMS(contact)
case "check_balance":
sms.CheckBalance(contact);
default:
sms.SendDirectSMS(contact)
}
}
}()
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
<- forever
}