-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.py
184 lines (142 loc) · 6.26 KB
/
index.py
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
# -*- coding: utf-8 -*-
from flask_cors import CORS, cross_origin
from flask import Response
from urllib.parse import urlparse
import re
import os
import json
import telebot
import configure.index as config
from flask import Flask, request, abort
import db
import configure.const as const
import urllib
bot = telebot.TeleBot(config.token)
app = Flask(__name__)
CORS(app, supports_credentials=True)
# Starting message
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, '<strong> ' + message.from_user.first_name + ', cпасибо, что установили нашего бота! ✌️</strong>\n\nVkFeedBot создан для работы ленты VK внутри Telegram.', parse_mode="HTML")
db.addUser(message.chat.id)
bot.send_message(int(message.chat.id), getCommandList(), parse_mode="HTML")
# Bot help
@bot.message_handler(commands=['help'])
def help(message):
bot.send_message(int(message.chat.id), getCommandList(), parse_mode="HTML")
# 'addlink' method
@bot.message_handler(commands=['addlink'])
def addlink(message):
bot.send_message(message.chat.id, "Введите URL группы, которую Вы хотели бы добавить?")
db.changeStatus(message.chat.id, const.status[const.ADDING_LINK])
# 'removelink' method
@bot.message_handler(commands=['removelink'])
def removelink(message):
bot.send_message(message.chat.id, "Введите URL группы, которую Вы хотели бы удалить?")
db.changeStatus(message.chat.id, const.status[const.REMOVING_LINK])
# Checking all text
@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
# Getting status
status = db.getStatus(message.chat.id)
if status == const.status[const.UNACTIVE]:
# If user is 'unactive'
bot.send_message(message.chat.id, "Ась?Не слышу!")
elif status == const.status[const.ADDING_LINK]:
message.text = message.text.lower()
if message.text == "отмена":
# If the cancel the action
db.changeStatus(message.chat.id, const.status[const.UNACTIVE])
bot.send_message(message.chat.id, "Добавление группы отменено.")
return
# If a person wants to add a group, then you need to analyze it and display the answer
if checkURL(message.text):
# Changinst status + add a group
db.changeStatus(message.chat.id, const.status[const.UNACTIVE])
# Checking status
linkStatus = db.addLink(pathParser(message.text).path[1:], message.chat.id)
if linkStatus == 0:
# "All" is ok
bot.send_message(message.chat.id, "Группа успешно добавлена ✅")
elif linkStatus == 1:
bot.send_message(message.chat.id, "Группа уже была добавлена")
elif linkStatus == -1:
bot.send_message(message.chat.id, "Произошла ошибка во время добавления группы")
else:
bot.send_message(message.chat.id, "Проверьте правильность введенный данных.\nДля выхода введите <strong>'Отмена'</strong>", parse_mode="HTML")
elif status == const.status[const.REMOVING_LINK]:
message.text = message.text.lower()
if message.text == "отмена":
# If we canceled all actions
db.changeStatus(message.chat.id, const.status[const.UNACTIVE])
bot.send_message(message.chat.id, "Добавление группы отменено.")
return
# If a person wants to delete a group, then you need to analyze it and display a response
if checkURL(message.text):
# Changes the status, if it finds a group, then deletes it
db.changeStatus(message.chat.id, const.status[const.UNACTIVE])
linkStatus = db.removeLink(pathParser(message.text).path[1:])
if linkStatus == 0:
# All is ok
bot.send_message(message.chat.id, "Группа успешно удалена ✅")
elif linkStatus == 1:
# Did not find such a group
bot.send_message(message.chat.id, "Такой группы нет 😲")
elif linkStatus == -1:
# If error
bot.send_message(message.chat.id, "Произошла ошибка во время добавления группы")
else:
bot.send_message(message.chat.id, "Проверьте правильность введенный данных.\nДля выхода введите <strong>'Отмена'</strong>", parse_mode="HTML")
else:
bot.send_message(message.chat.id, "Ась?Не слышу!")
def checkURL(url):
result = pathParser(url)
if result.path != "" and 'vk.com' in result.netloc:
return True
else:
return False
def pathParser(url):
# We check for correspondence to the url string
if '//' not in url:
url = '%s%s' % ('http://', url)
return urlparse(url)
# Help list
def getCommandList():
return "<strong>Команды бота:</strong>" + "\n\n" + "<a>/addlink</a> - Добавить группу VK\n" + "<a>/removelink</a> - Удалить группу VK"
'''
Near goes app work
'''
@app.route("/bot", methods=['POST'])
def getMessage():
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
return "!", 200
@app.route("/getsubs", methods=['POST', 'OPTIONS'])
@cross_origin()
def sendGroups():
''' Sending group data '''
result = db.getSubs()
# If where was an error
if result == -1:
abort(400)
return json.dumps(result)
@app.route("/setupdates", methods=['POST', 'OPTIONS'])
@cross_origin()
def setupdates():
''' The input receives the data of the group that you want to update '''
content = request.get_json()
for post in content['content']:
users = db.update(post, content['url'])
for user in users:
bot.send_message(user, "<strong>Поступило обновление с {}:</strong> \n\n {} \n\n {}".format(content['title'], post['text'], post['url']), parse_mode="HTML")
return json.dumps({'status': 'ok'})
@app.route("/")
def webhook():
bot.remove_webhook()
# app url
bot.set_webhook(url=config.token)
membsers = db.getCount("users")
subs = db.getCount("subs")
return "<strong>Количество пользователей:</strong> {}<br> <strong>Количество групп:</strong> {}".format(membsers, subs) , 200
# Available, but not for heroku
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get("PORT", 5000)))