forked from pdpfsug/MakerSpaceBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakerSpace.py
149 lines (116 loc) · 5.62 KB
/
MakerSpace.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
import os
import sys
import telepot
import time
from telepot.namedtuple import ReplyKeyboardMarkup, ReplyKeyboardRemove, InlineKeyboardMarkup, InlineKeyboardButton
from settings import TOKEN, start_msg, help_msg, info_msg, timeline_msg, msg_style
# State for user
user_state = {}
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
chat_id = msg['chat']['id']
command_input = msg['text']
# Getting information to print debug msg
try:
username = msg['from']['username']
except:
username = "Not defined"
try:
full_name = msg['from']['first_name'] + ' ' + msg['from']['last_name']
except:
full_name = "Not defined"
if username == "Not defined":
username = full_name
# Prints msg from the user
print("Msg from @{}[{}]: \"{}\"".format(username.ljust(16), chat_id, command_input))
# Commands
if command_input == "/start" or command_input == "/start@MakerSpaceFabrianoBot":
bot.sendMessage(chat_id, start_msg)
user_state[chat_id] = 0
elif command_input == "/help" or command_input == "/help@MakerSpaceFabrianoBot":
bot.sendMessage(chat_id, help_msg)
elif command_input == "/info" or command_input == "/info@MakerSpaceFabrianoBot":
bot.sendPhoto(chat_id, "https://i.imgur.com/reAMlzj.jpg")
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[dict(text = 'Pagina successiva >>', callback_data = 'info_next_page_1')]])
bot.sendMessage(chat_id, info_msg[0], reply_markup=keyboard, parse_mode = "Markdown")
elif (command_input == "Indietro" and user_state[chat_id] == 1):
bot.sendMessage(chat_id, start_msg, reply_markup = ReplyKeyboardRemove(remove_keyboard = True))
elif command_input == "/timeline" or command_input == "/timeline@MakerSpaceFabrianoBot" or (command_input == "Indietro" and user_state[chat_id] == 0):
markup = ReplyKeyboardMarkup(keyboard=[
["1950"],
["1966"],
["1972"],
["1988"],
["1992 - Parte 1"],
["1992 - Parte 2"],
["2000"],
["2005"],
["2006"],
["2007"],
["2015"],
["2016 - Parte 1"],
["2016 - Parte 2"],
["2016 - Parte 3"],
["2017 - Parte 1"],
["2017 - Parte 2"],
["Indietro"]])
bot.sendMessage(chat_id, timeline_msg, reply_markup=markup)
# Set the new user_state
user_state[chat_id] = 1
elif user_state[chat_id] == 1:
try:
markup = ReplyKeyboardMarkup(keyboard=[["Indietro"]], resize_keyboard=True)
bot.sendMessage(chat_id, msg_style[command_input], reply_markup=markup)
user_state[chat_id] = 0
except KeyError:
bot.sendMessage(chat_id, "La data inserita non è valida", reply_markup=ReplyKeyboardRemove(remove_keyboard=True))
user_state[chat_id] = 0
####################################################
# Inserire le tappe per la visualizzazione #
# delle informazioni relative all'Evoluzione #
# dei Chat Bot, link -> https://goo.gl/FXVbNu #
####################################################
else:
bot.sendMessage(chat_id, "Il messaggio inserito non è valido", reply_markup=ReplyKeyboardRemove(remove_keyboard=True))
def on_callback_query(msg):
"""
Function to manage callback query from callback buttons in inline keyboards
"""
query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
if query_data == 'info_next_page_0':
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[dict(text = 'Pagina successiva >>', callback_data = 'info_next_page_1')]])
bot.editMessageText((from_id, msg['message']['message_id']), info_msg[0], reply_markup = keyboard, parse_mode = "Markdown")
bot.answerCallbackQuery(query_id, text = "Buona lettura!")
if query_data == 'info_next_page_1':
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[dict(text = '<< Pagina precendente', callback_data = 'info_next_page_0'), dict(text = 'Pagina successiva >>', callback_data = 'info_next_page_2')]])
bot.editMessageText((from_id, msg['message']['message_id']), info_msg[1], reply_markup = keyboard, parse_mode = "Markdown")
bot.answerCallbackQuery(query_id, text = "Buona lettura!")
if query_data == 'info_next_page_2':
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[dict(text = '<< Pagina precendente', callback_data = 'info_next_page_1')]])
bot.editMessageText((from_id, msg['message']['message_id']), info_msg[2], reply_markup = keyboard, parse_mode = "Markdown")
bot.answerCallbackQuery(query_id, text = "Buona lettura!")
# PID file
pid = str(os.getpid())
pidfile = "/tmp/mkbot.pid"
# Check if PID exist
if os.path.isfile(pidfile):
print("%s already exists, exiting!" % pidfile)
sys.exit()
# Create PID file
f = open(pidfile, 'w')
f.write(pid)
f.close()
print('Vediamo quello che succede...')
try:
bot = telepot.Bot(TOKEN)
bot.message_loop({'chat': handle,
'callback_query': on_callback_query})
finally:
# Remove PID file on exit
os.unlink(pidfile)
while 1:
time.sleep(10)