-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
289 lines (259 loc) Β· 9.1 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"context"
"fmt"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/anaskhan96/soup"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
tbot "github.com/go-telegram-bot-api/telegram-bot-api"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var bot *tbot.BotAPI
//ButtonLinks sends the parsed arguements as an inline buttons to the specified chat ID
func ButtonLinks(ID int64, ButtonText string, ButtonURL string, MessageText string) {
var button = tbot.NewInlineKeyboardMarkup(
tbot.NewInlineKeyboardRow(
tbot.NewInlineKeyboardButtonURL(ButtonText, ButtonURL),
),
)
msg := tbot.NewMessage(ID, MessageText)
msg.ReplyMarkup = button
bot.Send(msg)
}
//Streams tweets with the help of Twitter API , bot sends the link of any new tweet or tweet related activity to the specified group on telegram.
func tweet() {
//Initializing twitter API
config := oauth1.NewConfig(os.Getenv("TWITTER_CONSUMER_KEY"), os.Getenv("TWITTER_CONSUMER_SECRET"))
token := oauth1.NewToken(os.Getenv("TWITTER_ACCESS_TOKEN"), os.Getenv("TWITTER_ACCESS_SECRET"))
httpClient := config.Client(oauth1.NoContext, token)
client := twitter.NewClient(httpClient)
params := &twitter.StreamFilterParams{
Follow: []string{os.Getenv("OSDC_TWITTER_ID")},
}
stream, _ := client.Streams.Filter(params)
demux := twitter.NewSwitchDemux()
demux.Tweet = func(tweet *twitter.Tweet) {
fmt.Println(tweet.IDStr)
GroupID, _ := strconv.ParseInt(os.Getenv("OSDC_GROUP_ID"), 10, 64)
bot.Send(tbot.NewMessage(GroupID, "New Tweet Alert: \n https://twitter.com/osdcjiit/status/"+tweet.IDStr))
}
demux.HandleChan(stream.Messages)
}
//Sends message of whatever the user specifies they are doing after they type this command, then deletes the user's command message.
func me(message *tbot.Message, ID int64) {
user := message.From.UserName
s := strings.SplitN(message.Text, " ", 2)
if len(s) == 2 {
bot.Send(tbot.NewMessage(ID, "* @"+user+" "+s[1]+" *"))
bot.DeleteMessage(tbot.NewDeleteMessage(ID, message.MessageID))
}
}
//scraping xkcd strip URL from its website with the help of a random generated integer and then sending it as a photo using NewPhotoShare Telegram API method.
func xkcd(ID int64) {
rand.Seed(time.Now().UnixNano())
min := 100
max := 2000
randomnum := (rand.Intn(max-min+1) + min)
xkcdurl := "https://xkcd.com/" + strconv.Itoa(randomnum)
resp, err := soup.Get(xkcdurl)
if err == nil {
doc := soup.HTMLParse(resp)
links := doc.Find("div", "id", "comic").FindAll("img")
for _, link := range links {
imglink := link.Attrs()["src"]
fullurl := "https:" + imglink
fmt.Println(fullurl)
bot.Send(tbot.NewPhotoShare(ID, fullurl))
}
} else {
fullurl := "https://imgs.xkcd.com/comics/operating_systems.png"
bot.Send(tbot.NewPhotoShare(ID, fullurl))
}
}
func help(ID int64) {
msg := ` Use one of the following commands to :
/github - Get a link to OSDC's Github page.
/telegram - Get an invite link for OSDC's Telegram Group.
/twitter - Get the link of OSDC's twitter account.
/website - Get the link of the official website of OSDC.
/blog - Get the link of the OSDC blog.
/irc - Find us on IRC.
/xkcd - Get a xkcd comic for you.
/dlmeetups - Get the list of upcoming meetups of Delhi/NCR communities.
/addmeetup* - Add the details of next OSDC Meetup.` + "\n" + `Format : /addmeetup <Title> <DD/MM/YYYY Hr:Min> <Venue>
/nextmeetup - Get the details of next OSDC Meetup
To contribute to|modify this bot : https://github.com/osdc/bots
* - Only for channel admins.
`
bot.Send(tbot.NewMessage(ID, msg))
}
//list of meetup groups urlnames,keeps sending one by one to getmeetups() function in meetup.go
func dlmeetups(ID int64) {
finallist = ""
urlnames := []string{"ilugdelhi", "pydelhi", "GDGNewDelhi", "gdgcloudnd", "Paytm-Build-for-India", "jslovers", "Gurgaon-Go-Meetup", "Mozilla_Delhi", "PyDataDelhi", "React-Delhi-NCR", "OWASP-Delhi-NCR-Chapter"}
for _, each := range urlnames {
url := "http://api.meetup.com/" + each + "/events"
getMeetups(url)
}
bot.Send(tbot.NewMessage(ID, finallist))
}
func welcome(user tbot.User, ID int64) {
User := fmt.Sprintf("[%v](tg://user?id=%v)", user.FirstName, user.ID)
var botSlice = make([]string, 0)
botSlice = append(botSlice,
"helping the hackers in here.",
"a bot made by the geeks for the geeks.",
"an Autobot on Earth to promote open source.",
"a distant cousin of the mars rover.",
"a friendly bot written in Go.",
)
var quesSlice = make([]string, 0)
quesSlice = append(quesSlice,
"which language do you work with?",
"what do you want to learn?",
"what is your current operating system?",
"if you are new to open source.",
"which technology fascinates you the most?",
"what have you been exploring recently?",
)
rand.Seed(time.Now().UnixNano())
min := 0
max := len(botSlice) - 1
randomNum1 := (rand.Intn(max-min+1) + min)
randomNum2 := (rand.Intn(max-min+1) + min)
var welcomeMessage1 = fmt.Sprintf(botSlice[randomNum1])
var welcomeMessage2 = fmt.Sprintf(quesSlice[randomNum2])
reply := tbot.NewMessage(ID, "Welcome "+User+", I am the OSDC-bot, "+welcomeMessage1+
" Please introduce yourself. To start with, you can tell us "+welcomeMessage2)
reply.ParseMode = "markdown"
bot.Send(reply)
}
//extracts the details of the user who sent the message to check whether the user is creator/admin. Returns true in this case else false.
func memberdetails(ID int64, userid int) bool {
response, _ := bot.GetChatMember(tbot.ChatConfigWithUser{
ChatID: ID,
UserID: userid,
})
if response.IsCreator() || response.IsAdministrator() {
return true
}
return false
}
func kickUser(user int, ID int64) {
bot.KickChatMember(tbot.KickChatMemberConfig{
ChatMemberConfig: tbot.ChatMemberConfig{
ChatID: ID,
UserID: user,
},
UntilDate: time.Now().Add(time.Hour * 24).Unix(),
})
}
func main() {
var err error
bot, err = tbot.NewBotAPI(os.Getenv("TELEGRAM_TOKEN"))
if err != nil {
fmt.Println("error in auth")
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tbot.NewUpdate(0)
u.Timeout = 60
updates, _ := bot.GetUpdatesChan(u)
// Set client options
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
// Check the connection
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected to MongoDB!")
// Running twitter stream concurrently
go tweet()
for update := range updates {
if update.Message == nil {
continue
}
ID := update.Message.Chat.ID
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
if update.Message.IsCommand() {
switch update.Message.Command() {
case "start":
ButtonLinks(ID, "Click Here", "https://t.me/jiitosdc", "Hello , I'm OSDC Bot, Use /help to know more. To join OSDC group:")
case "help":
help(ID)
case "github":
ButtonLinks(ID, "Github", "https://github.com/osdc", "Checkout our OSDC projects and feel free to contribute!")
case "telegram":
ButtonLinks(ID, "Click Here", "https://t.me/jiitosdc", "Join our telegram group:")
case "twitter":
ButtonLinks(ID, "Click Here", "https://twitter.com/osdcjiit", "Check us out on twitter")
case "website":
ButtonLinks(ID, "Click Here", "https://osdc.netlify.com", "Website:")
case "blog":
ButtonLinks(ID, "Click Here", "https://osdcblog.netlify.com/", "Blogs written by the folks at the Open Source Developers' Club who live in and around JIIT, Noida, India.")
case "irc":
ButtonLinks(ID, "Click Here", "https://github.com/osdc/community-committee/wiki/IRC", "Join us on IRC server of Freenode at #jiit-lug. To get started refer our IRC wiki-")
case "xkcd":
xkcd(ID)
case "dlmeetups":
dlmeetups(ID)
case "addmeetup":
check := memberdetails(ID, update.Message.From.ID)
if check == true {
addmeetup(ID, update.Message.Text, *client)
} else {
bot.Send(tbot.NewMessage(ID, "Sorry, only admins can add the details of next meetup."))
}
case "nextmeetup":
nextmeetup(ID, *client)
case "save":
check := memberdetails(ID, update.Message.From.ID)
if check == true {
savenote(ID, update.Message.Text, *client)
} else {
bot.Send(tbot.NewMessage(ID, "Sorry, only admins can add the details of next meetup."))
}
case "notes":
fetchallnotes(ID, *client)
case "deletenote":
deletenote(ID, update.Message.Text, *client)
case "fetchnote":
fetchnote(ID, update.Message.Text, *client)
case "paste":
paste(ID, update.Message)
case "me":
me(update.Message, ID)
default:
{
msg, err := bot.Send(tbot.NewMessage(ID, "I don't know this command"))
log.Print(err)
timer1 := time.NewTimer(5 * time.Second)
<-timer1.C
bot.DeleteMessage(tbot.NewDeleteMessage(ID, msg.MessageID))
}
}
}
if update.Message.NewChatMembers != nil {
for _, user := range *(update.Message.NewChatMembers) {
if user.IsBot && user.UserName != "osdcbot" {
go kickUser(user.ID, ID)
} else {
go welcome(user, ID)
}
}
}
}
}