-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtele_server.py
129 lines (99 loc) · 3.35 KB
/
tele_server.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 tkinter import messagebox
import mysql.connector as conn
import telebot
import io
import csv
try:
with open("tele_creds.txt",'r') as file:
contents = file.read().split("\n")
MYSQL_USERNAME = contents[0].partition(":")[2]
MYSQL_PASSWORD = contents[1].partition(":")[2]
API_TOKEN = contents[2].partition(":")[2]
file.close()
except FileNotFoundError:
messagebox.showerror("Error", "tele_creds.txt not found on current path!!")
CHAT_ID = ''
HELP = f"""COMMANDS:
/help - show this message
/view - view all books
/issued - view all issued books"""
db = conn.connect(host = 'localhost', user = MYSQL_USERNAME, password = MYSQL_PASSWORD, database='library')
cursor = db.cursor()
bot = telebot.TeleBot(API_TOKEN)
BOT_NAME = bot.get_me().first_name
def online_alert():
try:
cursor.execute("SELECT * FROM TELEBOT")
result = cursor.fetchall()
for i in result:
bot.send_message(chat_id=int(i[0]), text="Bot is Online!")
except:
pass
online_alert()
@bot.message_handler(commands = ['help'])
def help(message):
bot.send_message(message.chat.id, HELP)
@bot.message_handler(commands = ['start'])
def start(message):
cursor.execute("SELECT * FROM TELEBOT WHERE STUDENT_ID=%s",(message.chat.id,))
result = cursor.fetchall()
CHAT_ID = message.chat.id
if result == []:
cursor.execute("INSERT INTO TELEBOT(STUDENT_ID) VALUES(%s)", (message.chat.id,))
db.commit()
bot.send_message(message.chat.id, f"Welcome to {BOT_NAME}")
bot.send_message(message.chat.id, HELP)
@bot.message_handler(commands = ['view'])
def view(message):
cursor.execute("DESC books")
result = cursor.fetchall()
fields = []
for i in result:
fields.append(i[0])
cursor.execute("SELECT * FROM books")
data = cursor.fetchall()
# csv module can write data in io.StringIO buffer only
s = io.StringIO()
csv.writer(s).writerow(fields)
csv.writer(s).writerows(data)
s.seek(0)
# telebot library can send files only from io.BytesIO buffer
# we need to convert StringIO to BytesIO
buf = io.BytesIO()
buf.write(s.getvalue().encode())
buf.seek(0)
buf.name = f'BOOKS.csv'
bot.send_document(message.chat.id, document=buf)
@bot.message_handler(commands = ['issued'])
def generate_csv(query):
student_name = query.text
cursor.execute("DESC issued_books")
result = cursor.fetchall()
fields = []
for i in result:
fields.append(i[0])
cursor.execute("SELECT * FROM issued_books WHERE STUDENT_NAME=%s", (student_name,))
result = cursor.fetchall()
data = []
for i in result:
row = []
row.append(i[0])
row.append(i[1])
row.append(i[2])
row.append(i[3])
row.append(i[4])
row.append(i[5])
row.append(i[6])
row.append(i[7].strftime('%Y-%m-%d'))
row.append(i[8])
data.append(row)
s = io.StringIO()
csv.writer(s).writerow(fields)
csv.writer(s).writerows(data)
s.seek(0)
buf = io.BytesIO()
buf.write(s.getvalue().encode())
buf.seek(0)
buf.name = f'ISSUED_BOOKS.csv'
bot.send_document(query.chat.id, document=buf)
bot.infinity_polling()