-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
122 lines (112 loc) · 4.76 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
import os
import sys
import time
import telepot
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton, ForceReply
import json
import herokudb
import replyhandler
import helper
BOT_KEY = os.environ['BADR_BOT_KEY']
CHALLONGE_KEY = os.environ['BADR_CHALLONGE_KEY']
TOURNAMENT_ID = int(os.environ['BADR_TOURNAMENT_ID'])
ADMIN_ID = int(os.environ['BADR_ADMIN_ID']) #it's me
def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
if 'reply_to_message' in msg:#it's a reply
reply = replyhandler.handle(msg)
bot.sendMessage(chat_id, reply)
elif msg['text'].strip() == '/klasemen':
reply = 'KLASEMEN LIGA BADR 2017\n'
reply += '=======================\n'
players = herokudb.getStandings()
reply += ''
count = 1
for player in players:
reply += "{}. {} {}-{}-{} {}\n".format(
count, player[0], player[1], player[2], player[3], player[4])
count += 1
reply += '=======================\n'
reply += ('Klasemen di atas belum menggunakan tie break.'
' Versi lebih akurat cek: http://challonge.com/badrleague')
bot.sendMessage(chat_id, reply)
elif msg['text'].strip() == '/start' or msg['text'].strip() == '/help':
reply = "BADR(o)BOT v0.2.3\n"
reply += "Command yg tersedia:\n"
reply += "/matchinfo - melihat hasil/jadwal tanding player\n"
reply += "/klasemen - melihat klasemen saat ini\n"
reply += "/teamhistory - melihat histori tim yg digunakan player\n"
reply += "/updateskor - khusus admin (@yahyaman)\n\n"
reply += "Coming soon:\n"
reply += "(belum ada - silakan request)"
bot.sendMessage(chat_id, reply)
elif msg['text'].strip() == '/updateskor':
reply = 'Command ini khusus Admin! Situ siapa? :p'
force = None
if msg['from']['id'] == ADMIN_ID:
reply = 'Update skor dgn membalas chat ini:\n'
reply += 'player1 tim1 skor1 player2 tim2 skor2\n'
reply += 'Contoh:\n'
reply += 'yahya juve 2 amri chelsea 1'
force = ForceReply(force_reply=True, selective=True)
bot.sendMessage(chat_id, reply, reply_to_message_id=msg['message_id'], reply_markup=force)
elif msg['text'].strip() == '/teamhistory':
reply = 'Pilih player:'
players = herokudb.getPlayerList()
count = 0
buttons = []
btn_row = []
for player in players:
count += 1
data = "history-" + str(player[0])
btn_row.append(InlineKeyboardButton(text=player[1], callback_data=data))
if count % 3 == 0:
buttons.append(btn_row)
btn_row = []
#add last btns if present
if len(btn_row) > 0:
buttons.append(btn_row)
keyboard = InlineKeyboardMarkup(inline_keyboard=buttons)
bot.sendMessage(chat_id, reply, reply_markup=keyboard)
elif msg['text'].strip() == '/matchinfo':
reply = 'Pilih player:'
players = herokudb.getPlayerList()
buttons = helper.buildPlayerListButtons('match')
keyboard = InlineKeyboardMarkup(inline_keyboard=buttons)
bot.sendMessage(chat_id, reply, reply_markup=keyboard)
else:
bot.sendMessage(chat_id, "On progress!")
def on_callback_query(msg):
query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
print('Callback Query:', query_id, from_id, query_data)
msg_id = telepot.message_identifier(msg['message'])
bot.editMessageReplyMarkup(msg_id)
bot.answerCallbackQuery(query_id, text='Sip, oke!')
reply = "Maaf, ada kesalahan :("
if 'history' in query_data:
pid = int(query_data.split('-')[1])
name, teams = herokudb.getTeamHistory(pid)
reply = "Team history for {}:\n".format(name.upper())
for key, team in enumerate(teams):
reply += "{}. {} ({})\n".format(key+1, team[3].lower().title(), team[4][0].upper())
if len(teams) == 0:
reply += "(Belum ada)"
elif 'match' in query_data:
pid = int(query_data.split('-')[1])
name, results = herokudb.getMatchHistory(pid)
reply = "Match history for {}:\n".format(name.upper())
for record in results:
if record[7] == 'complete':
reply += "vs. {}: {}-{}\n".format(record[4], record[2], record[5])
else:
reply += "vs. {}: \n".format(record[4])
if len(results) == 0:
reply += "(Belum ada)"
#send result to existing message
bot.editMessageText(msg_id, reply)
bot = telepot.Bot(BOT_KEY)
bot.message_loop({'chat': on_chat_message,
'callback_query': on_callback_query})
print('Listening ...')
while 1:
time.sleep(10)