-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstart_and_admin.py
142 lines (105 loc) · 6.1 KB
/
start_and_admin.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
from pyrogram import Client, filters
from pyrogram.types import Message , InlineKeyboardButton , InlineKeyboardMarkup
from tabulate import tabulate
import pandas as pd
from stock import stock_run
import getpass
from telethon import TelegramClient, sync
from telethon.errors.rpcerrorlist import SessionPasswordNeededError
from telethon.tl.functions.contacts import ImportContactsRequest, DeleteContactsRequest
from telethon.tl.functions.users import GetFullUserRequest
from telethon.tl.types import InputPhoneContact, InputUser
def start_run():
api_id = 27356729
api_hash = "2076532de16fc82d242fcc1a012ce5f1"
bot_token = "6872044004:AAETNHH9kO-XnzfyeIIq1oTRNQNN4lnNr2Y"
#Define required arguments
bot = Client("start bot" ,
api_id = api_id ,
api_hash = api_hash ,
bot_token = bot_token ,
)
# Allowed list of admin usernames
admin_username = {
'username',
}
# Definition of the start command
@bot.on_message(filters.command('start') & filters.private)
def command1(bot , message):
first_name= message.from_user.first_name
keyboard = [
[InlineKeyboardButton("دریافت اطلاعات مشتریان", callback_data='reg_button'),
InlineKeyboardButton("ارسال پیام دسته جمعی" , callback_data='send_msg')]
]
reply_marks = InlineKeyboardMarkup(keyboard)
message.reply_text(f"سلام {first_name}! به ربات خوش اومدی.", reply_markup=reply_marks)
@bot.on_callback_query(filters.regex('reg_button$'))
def say_reg(client, callback_query):
callback_query.message.reply_text("/register")
# Admin panel definition
@bot.on_message(filters.command('admin') & filters.private)
def admin_command(client, message):
username = message.from_user.username
if username in admin_username: # If the desired username was in the admin list, we give the user access to administrative commands
keyboard = [
[InlineKeyboardButton("دریافت اطلاعات مشتریان", callback_data='get_user_info'),
InlineKeyboardButton("ارسال پیام دسته جمعی" , callback_data='send_msg')]
]
reply_mark = InlineKeyboardMarkup(keyboard)
message.reply_text("به پنل مدیریتی خوش آمدید", reply_markup=reply_mark)
else: # If the desired username is not in the admin list, we will send an error message to the user
message.reply_text("شما دسترسی به این دستور ندارید.")
# Send customer list to admin
@bot.on_callback_query(filters.regex('^get_user_info$'))
def get_user_info(client, callback_query):
try:
# Excel file path
file_path = 'users.xlsx'
# Reading customer information from an Excel file
df = pd.read_excel(file_path ,usecols= ['name', 'phone', 'email'])
# Set the output format using tabulate
user_info_text = tabulate(df, headers=['name', 'phone', 'email'], tablefmt="grid")
callback_query.message.edit_text(user_info_text)
# Send the Excel file to the user
client.send_document(chat_id=callback_query.message.chat.id, document=file_path)
except Exception as e:
callback_query.message.reply_text(f"خطایی رخ داد: {e}")
# از اینجا به بعد
@bot.on_callback_query(filters.regex('^send_msg$'))
async def send_customer_message(client, callback_query):
try:
phone = "+989160855428"
client = TelegramClient('find_chat_id', api_id, api_hash)
await client.connect()
if not await client.is_user_authorized():
print(12)
await client.send_code_request(phone)
code = input('Enter the Telegram code: ')
try:
await client.sign_in(phone, code)
except SessionPasswordNeededError:
password = getpass.getpass('Enter the password for the Telegram session ')
await client.sign_in(password=password)
Chat_IDs = []
unavailable_numbers = []
available_numbers = []
targets = 'targets.xlsx'
df = pd.read_excel(targets , usecols= ['phone_number'])
phone_numbers = df.values
for phone_number in phone_numbers:
contact = InputPhoneContact(client_id=0, phone=f'{phone_number}', first_name='Contacto', last_name='Temporal')
result = await client(ImportContactsRequest([contact]))
if result.users:
user = result.users[0]
user_id = user.id
Chat_IDs.append(user_id)
available_numbers.append(phone_number)
await client.send_message(user_id , "سلام. این یک پیام تست است")
print(f"پیام ارسال شدبه {phone_number}")
await client(DeleteContactsRequest(id=[InputUser(user_id=user.id, access_hash=user.access_hash)]))
else:
unavailable_numbers.append(phone_number)
await callback_query.message.reply_text(f"اتمام عملیات")
except Exception as e:
await callback_query.message.reply_text(f"خطایی رخ داد: {e}")
bot.run()