/
ia.go
137 lines (111 loc) · 3.5 KB
/
ia.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
cryptoRand "crypto/rand" // Alias to avoid name clash
"encoding/binary"
"log"
"math/rand"
"strings"
"time"
"github.com/ChimeraCoder/anaconda"
"github.com/jsgoecke/go-wit"
)
const (
INTENT_HI = "hi"
INTENT_NICE_ARTICLE = "nice_article"
INTENT_THANK_FOLLOW = "thank_follow"
)
// Initialize random seed and time zone
func init() {
// Better seeding for randomness
var b [8]byte
_, err := cryptoRand.Read(b[:])
if err != nil {
log.Fatal("Cannot seed math/rand package:", err)
}
rand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
// Load the Pacific Timezone
_, err = time.LoadLocation("America/Los_Angeles")
if err != nil {
log.Fatal(err)
}
}
func buildReply(tweet anaconda.Tweet) (string, error) {
message := cleanTweetMessage(tweet.Text)
if message == "" {
return "", nil
}
// Process a text message
request := &wit.MessageRequest{}
request.Query = message
result, err := witclient.Message(request)
if err != nil {
return "", err
}
// TODO(remy): quick fix
if len(result.Outcomes) == 0 {
return "", nil
}
outcome := result.Outcomes[0]
intent := outcome.Intent
if outcome.Confidence < 0.5 {
log.Println("Not enough confidence for intent : " + intent)
return "", nil
}
if intent == INTENT_HI {
return buildHiIntentResponse(tweet), nil
} else if intent == INTENT_NICE_ARTICLE {
return buildNiceArticleIntentResponse(tweet), nil
} else if intent == INTENT_THANK_FOLLOW {
return buildThanksFollowIntentResponse(tweet), nil
}
return "", nil
}
func buildHiIntentResponse(tweet anaconda.Tweet) string {
greetings := []string{"hello!", "hey", "yo"}
return buildMention(tweet.User, greetings[rand.Intn(len(greetings))])
}
func buildNiceArticleIntentResponse(tweet anaconda.Tweet) string {
greetings := []string{"hello!", "hey", "hi", "well,", ""}
thanks := []string{"thanks", "thank you", "many thanks", "thx"}
messages := []string{"reading", "your tweet", "your message"}
greet := greetings[rand.Intn(len(greetings))]
thank := thanks[rand.Intn(len(thanks))]
message := messages[rand.Intn(len(messages))]
return buildMention(tweet.User, greet+" "+thank+" for "+message)
}
func buildThanksFollowIntentResponse(tweet anaconda.Tweet) string {
greetings := []string{"hello!", "hey", "hi", "well,", ""}
thanks := []string{"thanks", "thank you", "many thanks", "thx"}
follows := []string{"following me", "the follow"}
messages := []string{"your message", "your tweet", "your mention"}
reciprocals := []string{"too", "as well", ""}
following, err := isUserFollowing(tweet.User.ScreenName)
if following && err == nil {
greet := greetings[rand.Intn(len(greetings))]
thank := thanks[rand.Intn(len(thanks))]
follow := follows[rand.Intn(len(follows))]
reciprocal := reciprocals[rand.Intn(len(reciprocals))]
return buildMention(tweet.User, greet+" "+thank+" for "+follow+" "+reciprocal)
} else {
greet := greetings[rand.Intn(len(greetings))]
thank := thanks[rand.Intn(len(thanks))]
message := messages[rand.Intn(len(messages))]
return buildMention(tweet.User, greet+" "+thank+" for "+message)
}
}
func buildMention(user anaconda.User, text string) string {
return "@" + user.ScreenName + " " + text
}
func cleanTweetMessage(message string) string {
cleaned := ""
words := strings.Split(message, " ")
for _, word := range words {
if strings.HasPrefix(word, "@") || strings.HasPrefix(word, "http") {
continue
} else if strings.HasPrefix(word, "#") {
cleaned += strings.TrimPrefix(word, "#") + " "
}
cleaned += word + " "
}
return cleaned
}