-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinout.go
167 lines (156 loc) · 4.24 KB
/
inout.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"time"
"www.samermurad.com/piBot/chatmachine"
"www.samermurad.com/piBot/telegram"
"www.samermurad.com/piBot/telegram/models"
"www.samermurad.com/piBot/timeutils"
"www.samermurad.com/piBot/util"
"www.samermurad.com/piBot/cmds"
"www.samermurad.com/piBot/config"
"www.samermurad.com/piBot/dispatch"
)
func attemptListeningToCmd(ch chan TrgmRes) {
cb := make(chan ([]*models.Update))
go telegram.GetUpdates(config.CHAT_OFFSET(), 15*time.Second, cb)
data := <-cb
ch <- TrgmRes{
Res: data,
Error: nil,
}
}
func Listener(dispatch chan<- *models.Update) {
updateRes := make(chan TrgmRes)
for {
boom := time.After(500 * time.Millisecond)
fmt.Println("Wait...")
<-boom
fmt.Println("Go!")
go attemptListeningToCmd(updateRes)
data := <-updateRes
if data.Error != nil {
fmt.Println("Error getting updates", data.Error)
} else {
if data.Res != nil {
first := data.Res[0]
config.SET_CHAT_OFFSET(first.UpdateId + 1)
dispatch <- first
}
}
}
}
type TelegramUpdateJob struct {
Done bool
ChatId int64
UpdateChannel <-chan interface{}
DoneChannel chan<- bool
ShutDownMesage string
}
func (tJ *TelegramUpdateJob) Start(updates <-chan interface{}, done chan<- bool) {
for u := range updates {
fmt.Println("Got Update")
tu := u.(*models.Update)
done <- tJ.Run(tu)
}
}
func (tJ *TelegramUpdateJob) ShutDown() {
fmt.Println("Shutting Down")
tmDebug(tJ.ShutDownMesage)
}
func (tJ *TelegramUpdateJob) Run(update *models.Update) bool {
if !util.IsChatAuthorized(update.Message.Chat.Id) {
// str := FetchRandomEvilInsult()
tJ.ShutDownMesage = "I Dont know you, go away"
return true
}
tmCmd, err := util.ParseCmdFromMessage(&update.Message)
if err != nil {
tJ.ShutDownMesage = "Bummer"
return true
} else if tmCmd == nil {
tJ.ShutDownMesage = "No Cmd for u!"
return true
} else if cmd := cmdMapping[tmCmd.Key]; cmd == nil {
tJ.ShutDownMesage = "Command not mapped"
return true
} else {
data := util.CmdExecData{
Message: &update.Message,
Cmd: tmCmd,
}
cmd.Exec(data)
return true
}
}
func Handler(cmdMapping map[string]cmds.Command, source <-chan *models.Update) {
startTime := timeutils.Seconds()
var creator func(update interface{}) (dispatch.Job, time.Duration)
creator = func(update interface{}) (dispatch.Job, time.Duration) {
tmUpdate, ok := update.(*models.Update)
if !ok {
return nil, 0
}
if startTime < tmUpdate.Message.Date {
return &chatmachine.ChatMachine{
ChatId: tmUpdate.Message.Chat.Id,
Cache: chatmachine.NewChatCache(tmUpdate.Message.Chat.Id, "Action Timed Out"),
States: map[string]chatmachine.ChatState{
"/ls": &chatmachine.LsState{},
"/media_structure": &chatmachine.OrganizeMedia{
SrcFolder: config.MEDIA_SRC_FOLDER(),
DestFolder: config.MEDIA_DEST_FOLDER(),
},
"/cmd": &chatmachine.RunSystemScript{
Cmd: "source $HOME/.bashrc && cpip",
Args: nil,
},
"/sleep": &chatmachine.RunSystemScript{
Cmd: "sleep 2 && echo \"Slept for 2 secs\"",
Args: nil,
},
"/sync_media": &chatmachine.RunSystemScript{
Cmd: "/home/pi/Developer/Bash/media_solution/sync",
},
"/whoami": &chatmachine.RunSystemScript{
Cmd: "whoami",
},
},
}, 10 * time.Second
} else {
fmt.Println("Ignoring old messages")
return nil, 0
}
return nil, 0
}
dispatch := dispatch.JobDispatcher{JobCreator: creator}
for {
fmt.Println("Handler before update := <-source")
update := <-source
dispatch.Dispatch(update.Message.Chat.Id, update)
// fmt.Println("Handler after update := <-source")
// if startTime < update.Message.Date {
// if !IsChatAuthorized(update.Message.Chat.Id) {
// str := FetchRandomEvilInsult()
// tmDebug(str)
// } else {
// tmCmd, err := ParseCmdFromMessage(&update.Message)
// if err != nil {
// tmDebug("Bummer")
// } else if tmCmd == nil {
// tmDebug("No Cmd for u!")
// } else if cmd := cmdMapping[tmCmd.Key]; cmd != nil {
// data := CmdExecData{
// Message: &update.Message,
// Cmd: tmCmd,
// }
// cmd.Exec(data)
// } else {
// tmDebug("Command not mapped")
// }
// }
// } else {
// fmt.Println("Ignoring old messages")
// }
}
}