-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTelegram.py
More file actions
195 lines (159 loc) · 8.77 KB
/
Telegram.py
File metadata and controls
195 lines (159 loc) · 8.77 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
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
import re
import configparser
import asyncio
from _thread import start_new_thread
from telebot import types
from telebot.async_telebot import AsyncTeleBot
from TooGoodToGo import TooGoodToGo
config = configparser.ConfigParser()
config.read('config.ini')
token = config['Telegram']['token']
bot = AsyncTeleBot(token)
tooGoodToGo = TooGoodToGo(token)
# Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start'])
async def send_welcome(message):
await bot.send_message(message.chat.id,
"""
*Hi welcome to the TGTG Bot:*
The bot will notify you as soon as new bags from your favorites are available.
*❗️️This is necessary if you want to use the bot❗️*
🔑 To login into your TooGoodToGo account enter
*/login email@example.com*
_You will then receive an email with a confirmation link.
You do not need to enter a password._
⚙️ With */settings* you can set when you want to be notified.
ℹ️ With */info* you can display all stores from your favorites where bags are currently available.
_🌐 You can find more information about Too Good To Go_ [here](https://www.toogoodtogo.com/).
*🌍 LET'S FIGHT food waste TOGETHER 🌎*
""", parse_mode="Markdown")
@bot.message_handler(commands=['info'])
async def send_info(message):
chat_id = str(message.chat.id)
credentials = tooGoodToGo.find_credentials_by_telegramUserID(chat_id)
if credentials is None:
await bot.send_message(chat_id=chat_id,
text="🔑 You have to log in with your mail first!\nPlease enter */login email@example.com*\n*❗️️This is necessary if you want to use the bot❗️*",
parse_mode="Markdown")
return None
tooGoodToGo.send_available_favourite_items_for_one_user(chat_id)
@bot.message_handler(commands=['login'])
async def send_login(message):
chat_id = str(message.chat.id)
credentials = tooGoodToGo.find_credentials_by_telegramUserID(chat_id)
if not credentials is None:
await bot.send_message(chat_id=chat_id, text="👍 You are always logged in!")
return None
email = message.text.replace('/login', '').lstrip()
print(email, " ", chat_id)
if re.match(r"[^@]+@[^@]+\.[^@]+", email):
await bot.send_message(chat_id=chat_id, text="📩 Please open your mail account"
"\nYou will then receive an email with a confirmation link."
"\n*You must open the link in your browser!*"
"\n_You do not need to enter a password._", parse_mode="markdown")
start_new_thread(tooGoodToGo.new_user, (chat_id, email))
else:
await bot.send_message(chat_id=chat_id,
text="*⚠️ No valid mail address ⚠️*"
"\nPlease enter */login email@example.com*"
"\n_You will then receive an email with a confirmation link."
"\nYou do not need to enter a password._",
parse_mode="Markdown")
def inline_keyboard_markup(chat_id):
inline_keyboard = types.InlineKeyboardMarkup(
keyboard=[
[
types.InlineKeyboardButton(
text=("🟢" if tooGoodToGo.users_settings_data[chat_id]["sold_out"] else "🔴") + " sold out",
callback_data="sold_out"
),
types.InlineKeyboardButton(
text=("🟢" if tooGoodToGo.users_settings_data[chat_id]["new_stock"] else "🔴") + " new stock",
callback_data="new_stock"
)
],
[
types.InlineKeyboardButton(
text=("🟢" if tooGoodToGo.users_settings_data[chat_id]["stock_reduced"] else "🔴") + " stock reduced",
callback_data="stock_reduced"
),
types.InlineKeyboardButton(
text=("🟢" if tooGoodToGo.users_settings_data[chat_id][
"stock_increased"] else "🔴") + " stock increased",
callback_data="stock_increased"
)
],
[
types.InlineKeyboardButton(
text="✅ activate all ✅",
callback_data="activate_all"
)
],
[
types.InlineKeyboardButton(
text="❌ disable all ❌",
callback_data="disable_all"
)
]
])
return inline_keyboard
@bot.message_handler(commands=['settings'])
async def send_settings(message):
chat_id = str(message.chat.id)
credentials = tooGoodToGo.find_credentials_by_telegramUserID(chat_id)
if credentials is None:
await bot.send_message(chat_id=chat_id,
text="🔑 You have to log in with your mail first!\nPlease enter */login email@example.com*\n*❗️️This is necessary if you want to use the bot❗️*",
parse_mode="Markdown")
return None
await bot.send_message(chat_id, "🟢 = enabled | 🔴 = disabled \n*Activate alert if:*", parse_mode="markdown",
reply_markup=inline_keyboard_markup(chat_id))
@bot.callback_query_handler(func=lambda c: c.data == 'sold_out')
async def back_callback(call: types.CallbackQuery):
chat_id = str(call.message.chat.id)
settings = tooGoodToGo.users_settings_data[chat_id]["sold_out"]
tooGoodToGo.users_settings_data[chat_id]["sold_out"] = 0 if settings else 1
await bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=call.message.message_id,
reply_markup=inline_keyboard_markup(chat_id))
tooGoodToGo.save_users_settings_data_to_txt()
@bot.callback_query_handler(func=lambda c: c.data == 'new_stock')
async def back_callback(call: types.CallbackQuery):
chat_id = str(call.message.chat.id)
settings = tooGoodToGo.users_settings_data[chat_id]["new_stock"]
tooGoodToGo.users_settings_data[chat_id]["new_stock"] = 0 if settings else 1
await bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=call.message.message_id,
reply_markup=inline_keyboard_markup(chat_id))
tooGoodToGo.save_users_settings_data_to_txt()
@bot.callback_query_handler(func=lambda c: c.data == 'stock_reduced')
async def back_callback(call: types.CallbackQuery):
chat_id = str(call.message.chat.id)
settings = tooGoodToGo.users_settings_data[chat_id]["stock_reduced"]
tooGoodToGo.users_settings_data[chat_id]["stock_reduced"] = 0 if settings else 1
await bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=call.message.message_id,
reply_markup=inline_keyboard_markup(chat_id))
tooGoodToGo.save_users_settings_data_to_txt()
@bot.callback_query_handler(func=lambda c: c.data == 'stock_increased')
async def back_callback(call: types.CallbackQuery):
chat_id = str(call.message.chat.id)
settings = tooGoodToGo.users_settings_data[chat_id]["stock_increased"]
tooGoodToGo.users_settings_data[chat_id]["stock_increased"] = 0 if settings else 1
await bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=call.message.message_id,
reply_markup=inline_keyboard_markup(chat_id))
tooGoodToGo.save_users_settings_data_to_txt()
@bot.callback_query_handler(func=lambda c: c.data == 'activate_all')
async def back_callback(call: types.CallbackQuery):
chat_id = str(call.message.chat.id)
for key in tooGoodToGo.users_settings_data[chat_id].keys():
tooGoodToGo.users_settings_data[chat_id][key] = 1
tooGoodToGo.save_users_settings_data_to_txt()
await bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=call.message.message_id,
reply_markup=inline_keyboard_markup(chat_id))
@bot.callback_query_handler(func=lambda c: c.data == 'disable_all')
async def back_callback(call: types.CallbackQuery):
chat_id = str(call.message.chat.id)
for key in tooGoodToGo.users_settings_data[chat_id].keys():
tooGoodToGo.users_settings_data[chat_id][key] = 0
tooGoodToGo.save_users_settings_data_to_txt()
await bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=call.message.message_id,
reply_markup=inline_keyboard_markup(chat_id))
asyncio.run(bot.polling())