This repository was archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgbot.coffee
114 lines (99 loc) · 4.36 KB
/
tgbot.coffee
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
BotAPI = require("teleapiwrapper").BotAPI
mongoose = require 'mongoose'
User = require './models/user'
Link = require './models/link'
Twit = require 'twit'
class TelegramBot
constructor: (config) ->
botToken = config.token
webHookUrl = config.web.domain
urlMongo = config.mongodb
twitter_auth = config.twitter
if botToken? and webHookUrl? and urlMongo? and twitter_auth?
@bot = new BotAPI botToken
@name = ''
@username = ''
self = @
@lastUpdateId = 0
@bot.getMe (err, res) ->
if err?
return console.log err
self.name = res.result.first_name
self.username = res.result.username.toLowerCase()
console.log "Hello, I am #{self.username}"
@bot.setWebhook webHookUrl ,(err, res) ->
if err?
return console.log err
console.log "WebHook url: #{webHookUrl}"
mongoose.connect urlMongo, (err, res) ->
if err
console.log 'ERROR connecting to: ' + urlMongo + '. ' + err
else
console.log 'Succeeded connected to: ' + urlMongo
#untalover
T = new Twit(twitter_auth)
stream1 = T.stream('statuses/filter', track: 'humblebundle gift')
stream1.on 'tweet', (tweet) ->
self.parseTweet tweet
stream1.on 'disconnect', (msg) ->
console.log msg
stream2 = T.stream('statuses/filter', track: 'humblebundle key')
stream2.on 'tweet', (tweet) ->
self.parseTweet tweet
stream2.on 'disconnect', (msg) ->
console.log msg
User.findOne {username: "OverJT"}, (err, user_obj) ->
if err
throw err
if user_obj?
self.bot.sendMessage user_obj.user_id, "Hola, me reinicié :3"
else
throw new Error("Not enough parameters provided. I need a token and a webhook url")
processMsg: (data) ->
self = @
if data.message.text?
match = data.message.text.match '^\/([a-zA-Z0-9_]{1,64})(?:@([a-zA-Z0-9_]{5,32}))?(?: (.*))?$'
if match?
if not match[2]? or match[2].toLowerCase() is @username
if match[1].toLowerCase() is "start"
msg = "¡Welcome, #{data.message.from.first_name}!\n\nAvailable commands:\n\n/help - Shows this message\n/enable - Allows the bot to send you the gift links as soon as they're published.\n/disable - Disables the messages"
@bot.sendMessage data.message.chat.id, msg
else if match[1].toLowerCase() is "enable"
User.findOneAndUpdate { user_id: data.message.from.id }, { username: data.message.from.username || '', user_fullname: data.message.from.first_name || '', alert_active: true },{upsert: true}, (err, user) ->
if err
throw err
if user is null
msg = "Hello #{data.message.from.first_name}\n¡You have enabled gift link alerts!\nYou can disable them by sending /disable"
else
msg = "¡You have enabled gift link alerts!\nYou can disable them by sending /disable"
self.bot.sendMessage data.message.chat.id, msg
else if match[1].toLowerCase() is "disable"
User.findOneAndUpdate { user_id: data.message.from.id }, { alert_active: false }, (err, user) ->
if err
throw err
self.bot.sendMessage data.message.chat.id, "¡You have disabled gift link alerts!\nyou can reenable them using the /enable command"
else if match[1].toLowerCase() is "help"
msg = "Available commands:\n\n/help - Shows this message\n/enable - Allows the bot to send you the gift links as soon as they're published.\n/disable - Disables the messages\n\nComments or suggestions contact @OverJT"
@bot.sendMessage data.message.chat.id, msg
parseTweet: (tweet) ->
self = @
user = tweet.user.screen_name
msg = tweet.text
urlRegex = /(https?:\/\/[^\s]+)/g
matchs = msg.match(urlRegex)
if matchs?
for link in matchs
Link.findOne { url: link }, (err, link_obj) ->
if err
throw err
if link_obj is null
User.find {alert_active: true}, (err, users_list) ->
if err
throw err
for user_obj in users_list
self.bot.sendMessage user_obj.user_id, "#{link}"
link_obj = new Link(
url: link
)
link_obj.save()
module.exports = TelegramBot