Created
May 28, 2022 13:25
-
-
Save Vostbur/c6283ebcd10fff4f2ea38b6692050a27 to your computer and use it in GitHub Desktop.
Birthday announcements Telegram bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
date | person | |
---|---|---|
28-05-1981 | Person One | |
28-05-1981 | Person Two | |
05-06-1980 | Person Three |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import time | |
from datetime import datetime | |
from multiprocessing.context import Process | |
import telebot | |
import schedule | |
API_TOKEN = 'YOURS_BOT_API_TOKEN' | |
USERID = 'CHAT_OR_USER_ID' | |
bot = telebot.TeleBot(API_TOKEN) | |
def send_message(): | |
with open('birthdays.csv', newline='') as csvfile: | |
reader = csv.DictReader(csvfile) | |
for row in reader: | |
datetime_object = datetime.strptime(row['date'], '%d-%m-%Y') | |
if datetime_object.month == datetime.now().month: | |
if datetime_object.day == datetime.now().day: | |
bot.send_message(USERID, row['person']) | |
schedule.every().day.at("08:00").do(send_message) | |
class ScheduleMessage: | |
@classmethod | |
def try_send_schedule(cls): | |
while True: | |
schedule.run_pending() | |
time.sleep(1) | |
@classmethod | |
def start_process(cls): | |
p1 = Process(target=ScheduleMessage.try_send_schedule, args=()) | |
p1.start() | |
if __name__ == '__main__': | |
ScheduleMessage.start_process() | |
try: | |
bot.polling(none_stop=True) | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment