-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
131 lines (96 loc) · 4.08 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
123
124
125
126
127
128
129
130
131
import logging
import os
from dotenv import load_dotenv
from jobCentre import JobCentre
from bruneida import Bruneida
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, Dispatcher)
# load the .env variables
load_dotenv()
# Setting up the bot api key from .env file
token = os.getenv("BOT_API_KEY")
# Setting up the logging for debugging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
# define the available states the telegram bot can be
INITIAL, SEARCH_VACANCIES_REPLY = range(2)
def search_vacancies(update, context):
# This is where we get the text the user keyed in
searched_keyword = update.message.text
# init new JobCentre class object
# platform = Bruneida()
platform = JobCentre()
# scrape the website based on the keyword
jobs = platform.scrape(searched_keyword)
# format the jobs array to nicer looking string
formatted_jobs_string = jobs_formatter(jobs)
# display the result to the user
update.message.reply_markdown(
"Your selected keyword is {}".format(searched_keyword)
)
update.message.reply_markdown(formatted_jobs_string)
return INITIAL
"""RUN THIS IF USER TEXTED /start"""
def start(update, context):
response = "Please enter your keyword"
# ask the user to enter the keyword
update.message.reply_markdown(response)
# return the state of the bot
return SEARCH_VACANCIES_REPLY
"""FORMAT THE JOBS ARRAY TO NICE STRINGS TO DISPLAY TO THE USER"""
def jobs_formatter(jobs):
formatted_jobs_string = ""
"""LOOP THE JOBS ARRAY"""
for job in jobs:
formatted_jobs_string += "\n*Company:* {}".format(job["company"])
formatted_jobs_string += "\n*Title:* [{}]({})".format(job["title"], job["link"])
formatted_jobs_string += "\n*Salary:* {}".format(job["salary"])
formatted_jobs_string += "\n[Apply Now]({})".format(job["applyLink"])
formatted_jobs_string += "\n\n"
"""RETURN THE FORMATTED STRING"""
return formatted_jobs_string
"""JUST TO LOG THE ERROR, NOT IMPORTANT"""
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
"""WHAT TO DO IF USER ENTERED INVALID COMMAND, NOT IMPORTANT"""
def fallback(update, context):
update.message.reply_markdown("Sorry, I couldn't understand your command")
return INITIAL
def main():
# put the updates in an Updater object
updater = Updater(token, use_context=True)
# dispatch the updates
dp = updater.dispatcher
# defining the handler
conv_handler = ConversationHandler(
# what to do when user start the program
entry_points=[
CommandHandler('start', start),
],
# definining the states your bot can assume
states={
# the INITIAL state, if your bot in this state, it could only understand the /start command
INITIAL: [
# what to do when the user sends /start to the bot
MessageHandler(Filters.regex('/start'), start),
# run the start() function when user sends /start
],
# if your bot in this state, it could only understand the keyword sent by the user
# sending /start to the bot if it is in this state will make it to query for /start keyword
# in job centre
SEARCH_VACANCIES_REPLY : [
# what to do when user replied with a keyword
MessageHandler(Filters.text, search_vacancies)
# run the search_vacancies() function when user reply with a keyword
],
},
# What to do if the bot don't understand anything
fallbacks=[MessageHandler(Filters.text, fallback)]
)
# add a handler to the Dispatcher object
dp.add_handler(conv_handler)
# poll telegram server and continuously request for updates
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()