-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg-birthday-from-google-calend.py
114 lines (100 loc) · 4.15 KB
/
tg-birthday-from-google-calend.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
# pip install asyncio datetime os random re time python-telegram-bot google-auth google-api-python-client openai python-dotenv
import asyncio
import datetime
import os
import random
import re
import time
import telegram
from google.oauth2.service_account import Credentials
from googleapiclient.errors import HttpError
from googleapiclient.discovery import build
import openai
from dotenv import load_dotenv
load_dotenv()
# Set your own values in the variables below and #60 .json file path/ Встановіть власні значення у змінні нижче та #60 строка шлях до .json файлу
GOOGLE_CALENDAR_ID = '[email protected]'
TELEGRAM_BOT_TOKEN = 'PUT_HERE_your_telegram_token'
# group like https://api.telegram.org/bot451456154:Affdsef-MdF-Hf89-4_ZXdfif7hnok/getUpdates
#chanell
TELEGRAM_GROUP_CHAT_ID = '-12345678945612' #or group '123456789'
OPENAI_API_KEY = os.getenv("HERE")
openai.api_key = "PUT_HERE" #is paid
async def get_greetings(name: str, description: str) -> str:
"""
Generating greetings using OpenAI API / Генерація поздоровлення за допомогою OpenAI API
"""
prompt = f"Generate greetings from colleagues for {name} on National Day with honors from 40-50 words and a line of words about {description}, then add the Emoji Telegram line to the text of the notification." \
f"Some notes"
while True:
try:
response = openai.Completion.create(
engine="text-davinci-003",
#engine="gpt-4", #not yet
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
timeout=120,
temperature=0.7,
top_p=1,
best_of=3
)
message = response.choices[0].text.strip()
message = re.sub(r"\n+", "\n", message)
return message
#except openai.error.APIError:
except (openai.error.ServiceUnavailableError, openai.error.APIError, openai.error.RateLimitError):
print("API Error occurred. Waiting for 20 minutes and trying again...")
time.sleep(1200)
def get_events_today() -> list:
"""
Get today's events from Google Calendar / Отримання подій сьогодні з Google Календаря
"""
credentials = Credentials.from_service_account_file(
"/home/user/file-from-calendar.json",
scopes=["https://www.googleapis.com/auth/calendar.readonly"],
)
service = build("calendar", "v3", credentials=credentials)
now = datetime.datetime.utcnow().isoformat() + "Z" # 'Z' indicates UTC time
today = datetime.datetime.now().date()
events_result = (
service.events()
.list(
calendarId=GOOGLE_CALENDAR_ID,
timeMin=now,
maxResults=10,
singleEvents=True,
orderBy="startTime",
)
.execute()
)
events = events_result.get("items", [])
today_events = []
if not events:
print("No events found.")
for event in events:
start = event["start"].get("date")
if start == today.strftime("%Y-%m-%d"):
today_events.append(event)
return today_events
async def send_telegram_message(message: str) -> None:
"""
Sending a message to a Telegram group / Надсилання повідомлення до Telegram групи
"""
bot = telegram.Bot(token=TELEGRAM_BOT_TOKEN)
await bot.send_message(chat_id=TELEGRAM_GROUP_CHAT_ID, text=message, parse_mode='Markdown')
async def main() -> None:
events_today = get_events_today()
if events_today:
message = f"Hello everyone 👋 Today we wish you a happy birthday: "
for event in events_today:
name = event["summary"]
description = event.get("description", "")
greetings = await get_greetings(name, description)
message += f"\n🎈*{name}* !) 🥳🎉\n{greetings}"
await send_telegram_message(message)
else:
print("There are no birth events today")
if __name__ == '__main__':
asyncio.run(main())