-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
156 lines (127 loc) · 4.81 KB
/
main.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
import logging
import re
from os import environ
from random import sample
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
ApplicationBuilder,
ContextTypes,
CommandHandler,
MessageHandler,
filters,
)
from telegram.constants import ParseMode, ChatAction
from telegram.error import TelegramError
from dotenv import load_dotenv
class CustomFormatter(logging.Formatter):
grey = "\x1b[38;20m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
blue = "\x1b[34;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: blue + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset,
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
logger = logging.getLogger("OiWA")
logger.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(CustomFormatter())
logger.addHandler(ch)
load_dotenv() # take environment variables from .env.
BOT_TOKEN = environ.get("BOT_TOKEN")
try:
assert BOT_TOKEN != None
except AssertionError:
logger.critical("Please set the environment variables")
exit(1)
GREATINGS = [
"Great! Here's your link",
"Sure thing! Take a look at this",
"Alright! You can find your link here",
"Here you go! Just click on the link below",
"No problem! I've got your link ready",
"Ready to go! Here's the link you need",
"Perfect! Your link is just a click away",
"Fantastic! You'll find your link right here",
"Alrighty! Look no further, your link awaits",
"Voilà! Here's the link you've been waiting for",
"Ta-da! Your link is all set up and ready",
"Behold! Your link is at your fingertips",
"Mission accomplished! Your link is here",
"Mission success! Here's the link you requested",
"Drumroll, please! Your link is here",
"Without further ado, here's your link",
"Your wish is my command! Here's your link",
"Eureka! I've found your link. Take a look",
"Look what I found! Your link is right here",
"And there you have it! Your link is ready to use",
]
async def cmd_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=f"Hello {update.effective_user.full_name} 👋🏻\nPlease send a phone number you want to chat with",
)
async def cmd_help(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="You can send the phone number you want to chat with (eg +447419651046)",
parse_mode=ParseMode.HTML,
)
async def wrong_number(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="❌ Wrong number",
)
async def main(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_chat_action(
chat_id=update.effective_chat.id, action=ChatAction.TYPING
)
try:
phone_number_regex = re.compile(
"(\+?( |-|\.)?\d{1,2}( |-|\.)?)?(\(?\d{3}\)?|\d{3})( |-|\.)?(\d{3}( |-|\.)?\d{4})"
)
extracted_phone_number = re.sub(r"[^\d]", "", update.effective_message.text)
if extracted_phone_number:
await phone_handler(update, context, extracted_phone_number)
else:
await wrong_number(update, context)
except (AttributeError, TelegramError) as err:
logging.error(f"🔴 Exception!: {err}\nupdate: {update}")
async def phone_handler(
update: Update, context: ContextTypes.DEFAULT_TYPE, phone_number: str
):
await context.bot.send_message(
text=sample(GREATINGS, 1)[0],
chat_id=update.effective_chat.id,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
text="🔗 Open chat",
url=f"https://api.whatsapp.com/send?phone={phone_number}&text=",
)
]
]
),
)
if __name__ == "__main__":
application = ApplicationBuilder().token(BOT_TOKEN).build()
text_handler = MessageHandler(filters.TEXT, main)
start_handler = CommandHandler("start", cmd_start)
help_handler = CommandHandler("help", cmd_help)
application.add_handler(start_handler)
application.add_handler(help_handler)
application.add_handler(text_handler)
application.run_polling()