-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
148 lines (122 loc) · 6.47 KB
/
bot.py
File metadata and controls
148 lines (122 loc) · 6.47 KB
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
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove
import logging
import os
import json
import requests
NAME = "lit-woodland-33204"
TOKEN = os.environ["TOKEN"]
PORT = os.environ.get("PORT", "5000")
updater = Updater(TOKEN)
dp = updater.dispatcher
def start(bot, update):
reply = "Hello " + str(update.message.from_user.username) + "! \n The available functions are: \n /start to start the bot\n/tiles to see all tiles, or to find tiles by suits\n/rules for rules in Mahjong, including ways to win"
bot.send_message(chat_id = update.message.chat_id, text=reply)
start_handler = CommandHandler('start', start)
dp.add_handler(start_handler)
# Other commands
def rules(bot, update):
reply = "In Mahjong, there are many ways to win. Some of the basic ones are: \n \
1) Ping Hu\n \
2) Peng Peng Hu\n \
3) Qing Yi Se\n \
4) Hun Yi Se \nWhich one do you want to know about?"
bot.send_message(chat_id=update.message.chat_id, text=reply)
rules_handler = CommandHandler('rules', rules)
dp.add_handler(rules_handler)
def win(bot, update):
reply = "Here are 4 main ways to win in Mahjong, which would you like to know about?"
keyboard = [
[ "Ping Hu", "Peng Peng Hu"],
[ "Qing Yi Se", "Hun Yi Se"]
]
reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)
bot.send_message(chat_id=update.message.chat_id, text=reply, reply_markup=reply_markup)
# bot.send_message(chat_id=update.message.chat_id, text="", reply_markup=ReplyKeyboardRemove(remove_keyboard=True, selective=False))
win_handler = CommandHandler('win', win)
dp.add_handler(win_handler)
def tiles(bot, update):
reply = "Here is a list of all the tiles in Mahjong! Choose what *suit(e)s* your interest!"
bot.send_message(chat_id=update.message.chat_id, text=reply)
bot.send_photo(chat_id=update.message.chat_id, photo="https://raw.githubusercontent.com/tirameshu/MahjongMaster/master/photos/tiles.jpg")
tiles_handler = CommandHandler('tiles', tiles)
dp.add_handler(tiles_handler)
# normal functions
def pinghu_reply(bot, chat_id):
bot.send_photo(chat_id=chat_id, photo="https://raw.githubusercontent.com/tirameshu/Mahjong_Master_python/master/photos/pinghu.png")
reply = "Above is an example of the Ping Hu (平胡) hand.\n \
Requirements:\n \
1) All *sets* must be sequential. No 3 of a kind.\n \
2) No honour tiles that can give multipliers, including winds.\n \
3) Waiting hand must be able to win with **at least** 2 different tiles.\n \
The hand below is not Ping Hu:"
return reply
def pengpenghu_reply(bot, chat_id):
bot.send_photo(chat_id=chat_id, photo="https://raw.githubusercontent.com/tirameshu/Mahjong_Master_python/master/photos/pengpenghu.png")
reply = "Above is an example of the Peng Peng Hu (碰碰胡) hand.\n \
Requirement:\n \
1) All *sets* must strictly be 3 (or 4) of a kind.\n \
The two sets on the side in the example are gang (杠, 4 of a kind) and peng (碰, 3 of a kind, one of which is the discard of another player)."
return reply
def qingyise_reply(bot, chat_id):
bot.send_photo(chat_id=chat_id, photo="https://raw.githubusercontent.com/tirameshu/Mahjong_Master_python/master/photos/qingyise.png")
reply = "Above is an example of the Qing Yi Se (清一色) hand\n \
Requirement:\n \
1) All *tiles* must be of the same suite. The sets can be a mix of sequential and 3 (or 4) of a kind."
return reply
def hunyise_reply(bot, chat_id):
bot.send_photo(chat_id=chat_id, photo="https://raw.githubusercontent.com/tirameshu/Mahjong_Master_python/master/photos/hunyise.png")
reply = "Above is an example of the Hun Yi Se (混一色) hand\n \
Requirement:\n \
1) *Tiles* must be of the same suite + honour tiles. The sets can be a mix of sequential and 3 (or 4) of a kind. \n \
The honour tiles can be either for the eyes (pair) or a set."
return reply
# General text responses
def respond(bot, update):
text = update.message.text
chat_id = update.message.chat_id
if text.lower() == "ping hu":
reply = pinghu_reply(bot, chat_id)
bot.send_message(chat_id=chat_id, text=reply)
bot.send_photo(chat_id=chat_id,
photo="https://raw.githubusercontent.com/tirameshu/Mahjong_Master_python/master/photos/not%20pinghu.png")
elif text.lower() == "peng peng hu":
reply = pengpenghu_reply(bot, chat_id)
bot.send_message(chat_id=chat_id, text=reply)
elif text.lower() == "qing yi se":
reply = qingyise_reply(bot, chat_id)
bot.send_message(chat_id=chat_id, text=reply)
elif text.lower() == "hun yi se":
reply = hunyise_reply(bot, chat_id)
bot.send_message(chat_id=chat_id, text=reply)
else:
reply = "Sorry! I can't really converse yet :("
bot.send_message(chat_id=chat_id, text=reply)
respond_handler = MessageHandler(Filters.text, respond)
dp.add_handler(respond_handler)
# when 'Tong', 'Dots'
# reply = "Here is a list of dotted tiles 筒子 in ascending order!"
# #bot.api.send_photo(chat_id: message.chat.id, photo:
# # Faraday::UploadIO.new('/Users/mandy/Repos/MahjongMaster/photos/dots.jpg', 'image/jpeg'))
# bot.api.send_message(chat_id: person_id, text: reply)
# when 'Tiao/Suo', 'Tiao', 'Suo', 'Bamboos'
# reply = "Here is a list of bamboo tiles 条子/ 索子 in ascending order!"
# #bot.api.send_photo(chat_id: message.chat.id, photo:
# # Faraday::UploadIO.new('/Users/mandy/Repos/MahjongMaster/photos/bamboos.jpg', 'image/jpeg'))
# bot.api.send_message(chat_id: person_id, text: reply)
# when 'Wan', 'Characters'
# reply = "Here is a list of character tiles 萬子 in ascending order!"
# #bot.api.send_photo(chat_id: message.chat.id, photo:
# # Faraday::UploadIO.new('/Users/mandy/Repos/MahjongMaster/photos/characters.jpg', 'image/jpeg'))
# bot.api.send_message(chat_id: person_id, text: reply)
# when 'DaPai', 'Honours'
# reply = "Here is a list of honour tiles 大牌 in no particular order!"
# #bot.api.send_photo(chat_id: message.chat.id, photo:
# # Faraday::UploadIO.new('/Users/mandy/Repos/MahjongMaster/photos/honours.jpg', 'image/jpeg'))
# bot.api.send_message(chat_id: person_id, text: reply)
# start webhooks
updater.start_webhook(listen="0.0.0.0",
port=int(PORT),
url_path=TOKEN)
updater.bot.setWebhook("https://{}.herokuapp.com/{}".format(NAME, TOKEN))
updater.idle()