Browse Source

First commit

Matteo Savatteri 4 years ago
commit
49b59496db
2 changed files with 51 additions and 0 deletions
  1. 21 0
      README.md
  2. 30 0
      wyh_bot.py

+ 21 - 0
README.md

@@ -0,0 +1,21 @@
+# `wyh_bot`
+This Telegram bot warns you to wash your hands.
+
+## Dependencies
+You need `python3` and `python-telegram-bot` python library to run `wyh_bot`.
+You can find its source code and install instruction at
+[github.com](https://github.com/python-telegram-bot/python-telegram-bot).
+
+## Install
+Clone this repo at your preferred path.
+
+## Configure
+Edit `wyh_bot.py` replacing `<BOT_TOKEN>` and `<CHAT_ID>`
+with your personal token and preferred Telegram chat id.
+Talk to `@botfather`.
+
+## Run
+```
+cd [path_to_repo]/wyh_bot
+./wyh_bot.py
+```

+ 30 - 0
wyh_bot.py

@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+import logging
+import telegram.ext
+from telegram.ext import Updater
+from time import localtime, sleep
+
+def callback(context: telegram.ext.CallbackContext):
+    if 0 <= localtime().tm_hour < 8:
+        context.job.enabled = False
+        return
+
+    context.bot.send_message(chat_id='<CHAT_ID>', 
+                             text='Did you wash your hands recently?')
+
+logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(7message)s',
+                     level=logging.INFO)
+
+updater = Updater(token='<BOT_TOKEN>', use_context=True)
+
+job_queue = updater.job_queue
+job_hour = job_queue.run_repeating(callback, interval=3600, first=0)
+
+updater.start_polling()
+
+while True:
+    if  localtime().tm_hour >= 8 and job_hour.enabled == False:
+        job_hour.enabled = True
+
+    sleep(300)