This repository has been archived by the owner on Feb 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversation.py
149 lines (125 loc) · 4.45 KB
/
conversation.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
#!coding:utf-8
# Created by bluebird on 2018/12/7
"""
docs
"""
from time import sleep
from telegram.ext import ConversationHandler
from config import SEND_SLEEP
from module import DBSession, Group
from module.user import User
from tool import command_wrap, check_admin, messaage_warp
from constant import RunState, USER_FORWARD_START, USER_FORWARD_STOP, NO_SUPPORT_FORMAT
from telegram.ext.filters import Filters
from telegram import Update, Bot, ParseMode
@command_wrap()
@check_admin()
def bcgroups(bot, update):
update.message.reply_text(text=USER_FORWARD_START)
return RunState.GRUOP_FORWARD
@command_wrap()
@check_admin()
def bcusers(bot, update):
update.message.reply_text(text=USER_FORWARD_START)
return RunState.USER_FORWARD
@command_wrap()
@check_admin()
def forwardusers(bot, update):
update.message.reply_text(text=USER_FORWARD_START)
return RunState.forwardusers
@command_wrap()
@check_admin()
def forwardgroups(bot, update):
update.message.reply_text(text=USER_FORWARD_START)
return RunState.forwardgroups
@command_wrap()
@check_admin()
def cancel(bot, update):
return ConversationHandler.END
@messaage_warp(filters=Filters.all)
def send_group(bot, update):
"""
:param bot:
:type bot: Bot
:param update:
:type update: Update
:return
"""
session = DBSession()
groups = session.query(Group).all()
send_admin_msg(users=groups, bot=bot, update=update)
bot.send_message(chat_id=update.message.chat_id, text=USER_FORWARD_STOP)
session.close()
return ConversationHandler.END
@messaage_warp(filters=Filters.all)
def send_user(bot, update):
session = DBSession()
users = session.query(User).all()
if not (Filters.text(update.message) | Filters.sticker(update.message) | Filters.photo(
update.message) | Filters.video(update.message)
| Filters.document(update.message)):
bot.send_message(chat_id=update.message.id, text=NO_SUPPORT_FORMAT)
return
send_admin_msg(users=users, bot=bot, update=update)
bot.send_message(chat_id=update.message.chat_id, text=USER_FORWARD_STOP)
session.close()
return ConversationHandler.END
@messaage_warp(filters=Filters.all)
def forward_user(bot, update):
session = DBSession()
users = session.query(User).all()
for user in users:
update.message.forward(user.id)
sleep(SEND_SLEEP)
bot.send_message(chat_id=update.message.chat_id, text=USER_FORWARD_STOP)
session.close()
return ConversationHandler.END
@messaage_warp(filters=Filters.all)
def forward_group(bot, update):
session = DBSession()
users = session.query(Group).all()
for user in users:
update.message.forward(user.id)
sleep(SEND_SLEEP)
bot.send_message(chat_id=update.message.chat_id, text=USER_FORWARD_STOP)
session.close()
return ConversationHandler.END
def send_admin_msg(users, bot, update):
if Filters.text(update.message):
for user in users:
bot.send_message(chat_id=user.id, text=update.message.text, parse_mode=ParseMode.HTML)
sleep(1)
elif Filters.sticker(update.message):
for user in users:
bot.send_sticker(chat_id=user.id, sticker=update.message.sticker.file_id)
sleep(1)
elif Filters.photo(update.message):
for photo in update.message.photo:
for user in users:
bot.send_photo(chat_id=user.id, photo=photo.file_id, caption=update.message.caption)
sleep(1)
elif Filters.video(update.message):
for user in users:
bot.send_video(chat_id=user.id, video=update.message.video.file_id, caption=update.message.caption)
sleep(1)
elif Filters.audio(update.message):
for user in users:
bot.send_audio(chat_id=user.id, audio=update.message.audio.file_id, caption=update.message.caption)
sleep(SEND_SLEEP)
elif Filters.document(update.message):
for user in users:
bot.send_document(chat_id=user.id, document=update.message.document.file_id,
caption=update.message.caption)
sleep(1)
else:
return
conv_handle = ConversationHandler(
entry_points=[bcgroups, bcusers, forwardusers, forwardgroups],
states={
RunState.forwardgroups: [forward_group],
RunState.forwardusers: [forward_user],
RunState.USER_FORWARD: [send_user],
RunState.GRUOP_FORWARD: [send_group]
},
fallbacks=[cancel]
)