This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook_listener.go
72 lines (61 loc) · 1.65 KB
/
webhook_listener.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
package main
import (
"log"
"regexp"
"strings"
"github.com/tomocrafter/go-twitter/twitter"
)
func listen(payloads chan interface{}) {
r := regexp.MustCompile(`<a href=".*?" rel="nofollow">(.*?)</a>`)
for event := range payloads {
switch t := event.(type) {
case twitter.TweetCreateEvent:
var builder strings.Builder
var isReply bool
// TODO: もっと効率いい方法があるはず。 @tomobotter @tomocrafter test | {0, 11}, {12, 23} | i >= e.Start() && i <= e.End() to @tomobotter @tomocrafter
var isTruncating bool
end := -1
for i, r := range t.Text {
for _, e := range t.Entities.UserMentions {
if id == e.ID {
isReply = true
}
if i == e.Indices.Start() {
isTruncating = true
end = e.Indices.End()
break
}
}
if isTruncating && i <= end {
continue
}
isTruncating = false
builder.WriteRune(r)
}
// if not retweet or not replyId or black listed.
via := r.FindStringSubmatch(t.Source)
if t.RetweetedStatus != nil || !isReply || (len(via) != 0 && isDeniedClient(via[1])) {
return
}
log.Println("TL @" + t.User.ScreenName + ": " + t.Text)
body := builder.String()
tweet := event.(twitter.Tweet)
go Dispatch(TimelineSender{
Tweet: &tweet,
}, body)
case twitter.DMEvent:
if string(id) == t.Message.SenderID {
return
}
text := t.Message.Data.Text
if t.Message.Data.QuickReplyResponse != nil {
text = t.Message.Data.QuickReplyResponse.Metadata
}
user := t.Users[t.Message.SenderID]
go Dispatch(DirectMessageSender{
User: &user,
DirectMessageEvent: &t.DirectMessageEvent,
}, text)
}
}
}