Skip to content

Instantly share code, notes, and snippets.

@wldhx
Created March 27, 2024 15:32
Show Gist options
  • Save wldhx/b6019dfc635fd954d3f38d4ce4c183d5 to your computer and use it in GitHub Desktop.
Save wldhx/b6019dfc635fd954d3f38d4ce4c183d5 to your computer and use it in GitHub Desktop.
telethon/openai event announcement rewriter
import asyncio
import os
import sys
import openai as openai_
import telethon
import telethon.sessions
PROMPT = """
A user gives you a long-form event announcement and you rewrite it to the following template:
⭐️ Event name
⏱ {start_time:%H:%M}-{end_time:%H:%M} {day_of_week}, {day} {month}
📍Venue, [address](maps link]
🗓 [registration](link) / cost
👅 language
For example:
📰 [Paper reading club: NLP potpourri](https://t.me/f0rthsp4ce/329)
⏱ 12:00-14:00 Saturday, 23 Mar
📍 [F0RTHSP4CE, Khorava St, 18](https://f0rth.space/visit-us.html)
🗓 no registration / free
👅 en+ru
"""
async def amain():
async with telethon.TelegramClient(
telethon.sessions.StringSession(os.environ["TELEGRAM_SESSION_TELETHON"]),
int(os.environ["TELEGRAM_API_ID"]),
os.environ["TELEGRAM_API_HASH"],
) as tg, openai_.AsyncClient() as openai:
text = sys.stdin.read()
print("EOF")
rewritten = await openai.chat.completions.create(
# model="gpt-3.5-turbo",
model="gpt-4-turbo-preview",
messages=[
{"role": "system", "content": PROMPT},
{"role": "user", "content": text},
],
)
await tg.send_message("me", rewritten.choices[0].message.content)
def main():
asyncio.run(amain())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment