Skip to content

Commit

Permalink
Fixed '/cancel' bug. Makefile command to create token env. Added docs…
Browse files Browse the repository at this point in the history
… and minor changes
  • Loading branch information
Charly98cma committed Sep 20, 2020
2 parents 52f5ec4 + 352a83f commit d5d516e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
init:
pip3 install -r requirements.txt
token:
export NF_TOKEN=$(cat token.txt)
run:
python3 nextflight_bot/nextflight.py

Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,28 @@ If you don't know how to do it, [this](https://www.makeuseof.com/tag/install-pip
This bot uses a couple of packages which are available on *pip*, see [requirements.txt](https://github.com/Charly98cma/Nextflight-bot/blob/master/requirements.txt) to see all of them.

The installation process of all the required packages has been changed to even more simple commands using the next command:
```

```makefile
make init
```

And... you're almost ready to take off :P

To run the bot, just execute:

```
```makefile
make run
```

## Credentials

To create yout bot, you must talk to the BotFather, which will guide you through the basics of setting up your bot, and will give you the token.

Once everything is set up, create a new environment variable called *NF_TOKEN* with your token, and execute the bot.
Once everything is set up, you have to create a new environment variable called *NF_TOKEN* with your token, which can be done executing the next command:

``` makefile
make token
```

## Acknowledgements

Expand Down
6 changes: 1 addition & 5 deletions nextflight_bot/flight_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,4 @@ def request_time_of(results, field, userTZ):
results[field],
"%Y-%m-%dT%H:%M:%SZ"
)
).astimezone(
userTZ[1]
).strftime(
"%Y/%m/%d - %H:%M:%S"
)
).astimezone(userTZ[0]).strftime("%Y/%m/%d - %H:%M:%S")
25 changes: 14 additions & 11 deletions nextflight_bot/nextflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,61 +29,66 @@
LOCATION, LOOP = range(2)

# List to save the TZ of the user (UTC by default)
userTZ = ['UTC', pytz.utc]
userTZ = [pytz.utc]
# Timezonefinder object
tf = TimezoneFinder()


def start_Command(update, context):
"""Start command explaining what can do this bot and asking for user location."""
logger.info('User {} starts a new conversation'.format(update.message.from_user.first_name))
msgMng.send_txtMsg(update, msgs.welcome_msg)
msgMng.send_txtMsg(update, msgs.location_msg)
return LOCATION


def location(update, context):
userTZ[0] = tf.timezone_at(
"""User shared location, and is used to 'calculate' its TZ."""
tz = tf.timezone_at(
lng = update.message.location["longitude"],
lat = update.message.location["latitude"]
)
userTZ[1] = pytz.timezone(userTZ[0])
msgMng.send_txtMsg(update, msgs.timezone_msg + userTZ[0])
logger.info('User {} timezone is {}'.format(update.message.from_user.first_name, userTZ[0]))
msgMng.send_txtMsg(update, msgs.timezone_msg + tz)
logger.info('User {} timezone is {}'.format(update.message.from_user.first_name, tz))
userTZ[0] = pytz.timezone(tz)
return LOOP


def skip_location(update, context):
"""User doesn't want to share location, so UTC will be used."""
logger.info('User {} didn\'t shared location'.format(update.message.from_user.first_name))
msgMng.send_txtMsg(update, msgs.skip_location_msg)
locFlag = True
return LOOP


def help_Command(update, context):
"""User asked for the list of commands."""
logger.info('User {} request the list of commands'.format(update.message.from_user.first_name))
msgMng.send_txtMsg(update, msgs.commands_msg)
return LOOP


def nextflight_Command(update, context):
"""User asked for information about the space flight."""
logger.info('User {} request next space flight info'.format(update.message.from_user.first_name))
next_msg, photo = next_Command(userTZ)
"""Based on the photo/infographic availability, the text does or doesn't include the photo"""
if photo is not None:
# Message with photo or infographic
msgMng.send_photoMsg(update, next_msg, photo)
else:
# Message without photo since it is not available
msgMng.send_txtMsg(update, next_msg)
return LOOP


def unknown_Command(update, context):
"""User introduced an unknown command."""
logger.info('User {} send an unknown command {}'.format(update.message.from_user.first_name, update.message.text))
msgMng.send_txtMsg(update, msgs.unknown_msg)
return LOOP


def cancel_Command(update, context):
"""Cancel command to end the conversation with the bot."""
logger.info('User {} ended conversation'.format(update.message.from_user.first_name))
msgMng.send_txtMsg(update, msgs.cancel_msg)
return ConversationHandler.END
Expand All @@ -95,7 +100,6 @@ def error(update, context):


def main():

if 'NF_TOKEN' not in os.environ:
print("Environment variable 'NF_TOKEN' not defined.", file=sys.stderr)
exit(1)
Expand All @@ -111,7 +115,6 @@ def main():
# Handlers
conv_handler = ConversationHandler(
entry_points = [CommandHandler('start', start_Command)],

states = {
LOCATION : [
MessageHandler(Filters.location, location),
Expand All @@ -120,10 +123,10 @@ def main():
LOOP : [
CommandHandler('help', help_Command),
CommandHandler('nextflight', nextflight_Command),
CommandHandler('cancel', cancel_Command),
MessageHandler(Filters.command, unknown_Command)
]
},

fallbacks = [
CommandHandler('cancel', cancel_Command)
],
Expand Down

0 comments on commit d5d516e

Please sign in to comment.