-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
129 lines (107 loc) · 4.16 KB
/
bot.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
from aiogram import Bot, Dispatcher, types, F
from aiogram.fsm.context import FSMContext
from aiogram.filters import CommandStart, Command
from aiogram.enums import ParseMode
import logging
import asyncio
import yagmail
import re
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from config import token, admin
from filters import Channel
from state import Email
from buttons import email_kb, hy
email_regex = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
# Вход в аккаунт Gmail
# yag = yagmail.SMTP('aizensbp086@gmail.com', 'supershox0909')
def is_valid_email(email):
return re.match(email_regex, email) is not None
logging.basicConfig(level=logging.INFO)
bot = Bot(token)
dp = Dispatcher()
@dp.message(CommandStart())
async def cmd_start(message: types.Message, state: FSMContext):
if not Channel():
await message.answer(text='Kanalga obuna boling 👤', reply_markup=email_kb)
else:
await message.answer(f'<b>👋 Salom {message.from_user.first_name}\nBu bot orqali siz Emailinguzga xabar yuboraolasiz 💬\n\nEmailni kiriting ✍️</b>', parse_mode=ParseMode.HTML)
await state.set_state(Email.email)
@dp.callback_query(F.data == 'ch')
async def ch(call: types.CallbackQuery):
if not Channel():
await call.answer('Kanalga obuna boldingiz')
else:
await call.answer(text='Kanalga obuna boling', show_alert=True)
@dp.message(F.text, Email.email)
async def email(message: types.Message, state: FSMContext):
email = message.text
if is_valid_email(email):
await state.update_data(
{
'email': email
}
)
await state.set_state(Email.subject)
await message.answer(f'{email} ga email tuboramiz\n\n Endi qaysi mavzuga yuboray')
else:
await message.answer('Faqat Email')
await state.clear()
@dp.message(F.text, Email.subject)
async def sub(message: types.Message, state: FSMContext):
subject = message.text
await state.update_data(
{
'subject':subject
}
)
await state.set_state(Email.body)
await message.answer('Endi xabar yuboring faqat text')
@dp.message(F.text, Email.body)
async def body(message: types.Message, state: FSMContext):
body = message.text
await state.update_data(
{
'body':body
}
)
await state.set_state(Email.conf)
await message.answer('Malumotlar saqlandi elarni yuboraymi? 📝', reply_markup=hy)
@dp.callback_query(F.data, Email.conf)
async def conf(call: types.CallbackQuery, state: FSMContext):
data = call.data
if data == 'true':
msg = await state.get_data()
email = msg.get('email')
subject = msg.get('subject')
body = msg.get('body')
sender_email = "email_senderp16@mail.ru"
receiver_email = email
password = "86hVQGvMUesJ2GdEi00p"
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
body = body
msg.attach(MIMEText(body, 'plain'))
# yag.send(to=email, subject=subject, contents=body)
await call.message.answer(f'Email: {email} 📬\nMavzu: {subject} 📝\nXabar: {body} 💬\n\nYuborildi ✅')
try:
with smtplib.SMTP('smtp.mail.ru', 587) as server: # Замените на ваш SMTP-сервер
server.starttls() # Защита соединения
server.login(sender_email, password) # Логин
server.send_message(msg) # Отправка сообщения
print("Сообщение успешно отправлено")
except Exception as e:
print(f"Ошибка: {e}")
else:
await state.clear()
await call.answer(text='Yuborilmadi ❌', show_alert=True)
async def main():
await dp.start_polling(bot)
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
print('Bot stopped')