-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbot.py
349 lines (243 loc) · 9.13 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# This bot script was written by Codefred from Fiverr
# A luckydraw bot
# Importing the needed libraries
import os
import telebot
from telebot import types
import emoji
from random import choice
import telegram
from datetime import date
from flask import Flask, request
# Initializing the bot
token = '' # <<---- INPUT YOUR BOT TOKEN
# Needed variables for the application
message_id = '' #previous message to be deleted
luckyNumbers = [choice(range(1,501)) for i in range(30)] # random 30 numbers in range(500)
drawNumbers = [i for i in range(1,501)]
winners = {} # dictionary of winners
members = [] # username of lucky draws
participants = {} # Unlucky memebers
chat = '' # Instance Chat
user = '' # Instance User
bot = telebot.TeleBot(token=token, threaded=True)
# Server
server = Flask(__name__)
@bot.message_handler(commands=['start'])
def start(msg):
"Sending Welcome to Bot Message"
global user
global chat
global message_id
if msg.from_user.username is None:
bot.send_message("Sorry! You are only qualified for a lucky draw if you have username.")
else:
chat = msg.chat.id
user = msg.from_user
message_id = msg.message_id
beginning()
def beginning():
"""
Starting the Lottery
"""
global message_id
# Keyboard Input
keyboard = types.InlineKeyboardMarkup(row_width=2)
a = types.InlineKeyboardButton(text=emoji.emojize(":gift:Lucky Draw:gift:", use_aliases=True), callback_data="1")
keyboard.add(a)
# Ask Request
bot.send_message(
user.id,
emoji.emojize(f"""
Hello {user.username},
:fire: Welcome to JanjiLuckyBot :fire:
""",
use_aliases=True),
reply_markup=keyboard
)
# Deleting previous message
bot.delete_message(chat, message_id)
message_id = int(message_id) + 1
return message_id
def get_lucky():
"Identifying Each User For The Lucky Draw"
global message_id
#Keyboard
keyboard = types.InlineKeyboardMarkup(row_width=1)
a = types.InlineKeyboardButton(text=emoji.emojize(":slot_machine: Try Your Luck :slot_machine:", use_aliases=True), callback_data="2")
b = types.InlineKeyboardButton(text=emoji.emojize(" Back To Menu :arrow_left:", use_aliases=True), callback_data="3")
keyboard.add(a, b)
# Lucky draw board
bot.send_message(
user.id,
emoji.emojize(f"""
:fire: JanjiLuckyBot Lottery :fire:
:hourglass: <b>Event</b>: 00.00AM ~ 23.59PM (16/2/2020)
:alarm_clock: <b>At 12PM, new winners can claim</b>
:gift: <b>30 Gifts 5 Credits Free ID</b>
Contact at ; @JanjiLuckyBot
:one: Your Lucky Number - A Lucky Draw Number would be generated for you when you press the <b>Try Your Luck</b> button
:two: Gifts - Gifts provided by Admin, each gift has its own <b>Lucky Number</b>
:three: If your <b>Lucky Number</b> is the same as the gift code number, your name will be on the <b>Winners Member</b> List and eligible for the prize
:four: To those customers whose <b>Lucky Draw Number</b> doesn't match the gift code, your name will be on the <b>Last 20 Participants</b> list
""",
use_aliases=True),
parse_mode=telegram.ParseMode.HTML,
reply_markup=keyboard
)
# Deleting previous message
bot.delete_message(chat, message_id)
message_id = int(message_id) + 1
return message_id
def lottery():
"Identifying Each User For The Lucky Draw"
global message_id
global members
global winners
global participants
if user.username not in members:
#register user into members
userNumber = choice(drawNumbers)
drawNumbers.remove(userNumber)
members.append(user.username) # Update Users Registered
# Update user information
dict = {user.username: userNumber}
# Check if he/her is lucky
if userNumber in luckyNumbers:
winners.update(dict)
else:
participants.update(dict)
display_result()
def display_result():
"""
Lottery Results
"""
global message_id
# Deleting previous message
bot.delete_message(chat, message_id)
#Keyboard
keyboard = types.InlineKeyboardMarkup(row_width=1)
a = types.InlineKeyboardButton(text=emoji.emojize(":phone: Contact :phone:", use_aliases=True), callback_data="4")
b = types.InlineKeyboardButton(text=emoji.emojize(" Back To Menu :arrow_left:", use_aliases=True), callback_data="5")
keyboard.add(a, b)
today = date.today()
if user.username in winners:
value = winners[user.username]
else:
value = participants[user.username]
win_data = ["@{}(Lucky No. {}), ".format(each, winners[each]) for each in list(winners.keys())]
loss_data = ["@{}(Lucky No. {}), ".format(each, participants[each]) for each in list(participants.keys())] # Lossers list
# Main Draw board
bot.send_message(
user.id,
emoji.emojize(f"""
:slot_machine: JanjiLuckyBot Lottery :slot_machine:
:alarm_clock: <b>{today} Lucky Draw Event</b>
:one: If your name is on the <b>Winner Member List</b>, it means you have won the prize
:two: Claim by contacting ---- between 11.59PM - 12AM ({today}), after which no claim can be made again
:8ball: <b>Your Lucky Number is {value}</b>
<b>
Gifts:
No 1. FREE 5 (Lucky no: {luckyNumbers[0]})
No 2. FREE 5 (Lucky no: {luckyNumbers[1]})
No 3. FREE 5 (Lucky no: {luckyNumbers[2]})
No 4. FREE 5 (Lucky no: {luckyNumbers[3]})
No 5. FREE 5 (Lucky no: {luckyNumbers[4]})
No 6. FREE 5 (Lucky no: {luckyNumbers[5]})
No 7. FREE 5 (Lucky no: {luckyNumbers[6]})
No 8. FREE 5 (Lucky no: {luckyNumbers[7]})
No 9. FREE 5 (Lucky no: {luckyNumbers[8]})
No 10. FREE 5 (Lucky no: {luckyNumbers[9]})
No 11. FREE 5 (Lucky no: {luckyNumbers[10]})
No 12. FREE 5 (Lucky no: {luckyNumbers[11]})
No 13. FREE 5 (Lucky no: {luckyNumbers[12]})
No 14. FREE 5 (Lucky no: {luckyNumbers[13]})
No 15. FREE 5 (Lucky no: {luckyNumbers[14]})
No 16. FREE 5 (Lucky no: {luckyNumbers[15]})
No 17. FREE 5 (Lucky no: {luckyNumbers[16]})
No 18. FREE 5 (Lucky no: {luckyNumbers[17]})
No 19. FREE 5 (Lucky no: {luckyNumbers[18]})
No 20. FREE 5 (Lucky no: {luckyNumbers[19]})
No 21. FREE 5 (Lucky no: {luckyNumbers[20]})
No 22. FREE 5 (Lucky no: {luckyNumbers[21]})
No 23. FREE 5 (Lucky no: {luckyNumbers[22]})
No 24. FREE 5 (Lucky no: {luckyNumbers[23]})
No 25. FREE 5 (Lucky no: {luckyNumbers[24]})
No 26. FREE 5 (Lucky no: {luckyNumbers[25]})
No 27. FREE 5 (Lucky no: {luckyNumbers[26]})
No 28. FREE 5 (Lucky no: {luckyNumbers[27]})
No 29. FREE 5 (Lucky no: {luckyNumbers[28]})
No 30. FREE 5 (Lucky no: {luckyNumbers[29]})
</b>
:heavy_minus_sign: :heavy_minus_sign: :heavy_minus_sign: :heavy_minus_sign: :heavy_minus_sign: :heavy_minus_sign:
:trophy: <b>Winner Members List</b> :trophy:
{','.join(each for each in win_data)}
:heavy_minus_sign: :heavy_minus_sign: :heavy_minus_sign: :heavy_minus_sign: :heavy_minus_sign: :heavy_minus_sign:
:busts_in_silhouette: <b>Winner Members List</b>
{','.join(each for each in loss_data[:20])}
""",
use_aliases=True),
parse_mode=telegram.ParseMode.HTML,
reply_markup=keyboard
)
message_id = int(message_id) + 1
def contact():
"""
Contact information
"""
global message_id
# Deleting previous message
bot.delete_message(chat, message_id)
#Keyboard
keyboard = types.InlineKeyboardMarkup(row_width=1)
a = types.InlineKeyboardButton(text=emoji.emojize(" Back To Menu :arrow_left:", use_aliases=True), callback_data="2")
keyboard.add(a)
# Main Draw board
bot.send_message(
user.id,
emoji.emojize(f"""
:fire: JanjiLuckyBot Contact :fire:
:alarm_clock: <b>Telegram</b>:
:alarm_clock: <b>WhatsApp</b>:
:alarm_clock: <b>WeChat</b>:
:alarm_clock: <b>Website</b>:
""",
use_aliases=True),
parse_mode=telegram.ParseMode.HTML,
reply_markup=keyboard
)
message_id = int(message_id) + 1
# Callback Handlers
@bot.callback_query_handler(func=lambda call: True)
def callback_answer(call):
"""
Button Response
"""
if call.data == "1":
get_lucky()
elif call.data == "2":
lottery()
elif call.data == "3":
beginning()
elif call.data == "4":
contact()
elif call.data == "5":
get_lucky()
else:
bot.send_message(user.id, "Wrong Input Content!! ")
# Development server
print("Bot running.....")
bot.remove_webhook()
bot.polling(none_stop=True)
# # Production server
# @server.route('/' + token, methods=['POST'])
# def getMessage():
# bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
# return "Lucky Draw Bot Running!", 200
# @server.route("/")
# def webhook():
# bot.remove_webhook()
# bot.set_webhook(url='https://obscure-island-36357.herokuapp.com/' + token)
# return "Lucky Draw Bot Active!", 200
# if __name__ == "__main__":
# server.run()