Skip to content

Commit

Permalink
Merge pull request #1319 from nconsigny/master
Browse files Browse the repository at this point in the history
multiple fix
  • Loading branch information
nconsigny authored Feb 21, 2025
2 parents 5179971 + 450b767 commit 95c268d
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 90 deletions.
16 changes: 15 additions & 1 deletion .github/ACDbot/meeting_topic_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,19 @@
"upload_attempt_count": 0,
"transcript_attempt_count": 0,
"calendar_event_id": "MmpkcHAzY3JicTViY3V2OWdsaHRuZ2dxbDQgY191cGFvZm9uZzhtZ3JtcmtlZ243aWM3aGs1c0Bn"
},
"88167935495": {
"discourse_topic_id": 22931,
"issue_title": "ACDbot test email notif to facilitator",
"start_time": "2025-02-24T03:00:00Z",
"duration": 35,
"issue_number": 14,
"meeting_id": "88167935495",
"Youtube_upload_processed": false,
"transcript_processed": false,
"upload_attempt_count": 0,
"transcript_attempt_count": 0,
"calendar_event_id": "b2F0NDVvcTN2cGw0a2p1aWVsYmZ1M2xjMGMgY191cGFvZm9uZzhtZ3JtcmtlZ243aWM3aGs1c0Bn",
"telegram_message_id": 42
}
}
}
15 changes: 15 additions & 0 deletions .github/ACDbot/modules/gcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def create_event(summary: str, start_dt, duration_minutes: int, calendar_id: str
Creates a Google Calendar event using the Google Calendar API.
Handles both datetime objects and ISO format strings for start_dt.
"""
print(f"[DEBUG] Creating calendar event: {summary}")

# Convert start_dt to datetime object if it's a string
if isinstance(start_dt, str):
Expand Down Expand Up @@ -43,11 +44,13 @@ def create_event(summary: str, start_dt, duration_minutes: int, calendar_id: str
service = build('calendar', 'v3', credentials=credentials)

event = service.events().insert(calendarId=calendar_id, body=event_body).execute()
print(f"[DEBUG] Created calendar event with ID: {event.get('id')}")

return event.get('htmlLink')

def update_event(event_id: str, summary: str, start_dt, duration_minutes: int, calendar_id: str, description=""):
"""Update an existing Google Calendar event"""
print(f"[DEBUG] Updating calendar event {event_id} with summary: {summary}")

# Same datetime handling as create_event
if isinstance(start_dt, str):
Expand All @@ -73,10 +76,22 @@ def update_event(event_id: str, summary: str, start_dt, duration_minutes: int, c

service = build('calendar', 'v3', credentials=credentials)

try:
# First try to get the event to verify it exists
existing_event = service.events().get(
calendarId=calendar_id,
eventId=event_id
).execute()
print(f"[DEBUG] Found existing event with ID: {existing_event.get('id')}")
except Exception as e:
print(f"[DEBUG] Failed to find existing event: {str(e)}")
raise # Re-raise the exception to be handled by the caller

event = service.events().update(
calendarId=calendar_id,
eventId=event_id,
body=event_body
).execute()
print(f"[DEBUG] Successfully updated event with ID: {event.get('id')}")

return event.get('htmlLink')
29 changes: 0 additions & 29 deletions .github/ACDbot/modules/telegram.py

This file was deleted.

97 changes: 97 additions & 0 deletions .github/ACDbot/modules/tg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import os
import requests
from telegram.constants import ParseMode

ZOOM_CLIENT_ID = os.environ.get("ZOOM_CLIENT_ID")
ZOOM_CLIENT_SECRET = os.environ.get("ZOOM_CLIENT_SECRET")
ZOOM_ACCOUNT_ID = os.environ.get("ZOOM_ACCOUNT_ID")

DISCOURSE_API_KEY = os.environ.get("DISCOURSE_API_KEY")
DISCOURSE_API_USERNAME = os.environ.get("DISCOURSE_API_USERNAME")
DISCOURSE_BASE_URL = os.environ.get("DISCOURSE_BASE_URL")


def send_message(text: str):
"""
Sends a message to a Telegram channel or group.
Returns the message ID for future updates.
"""
token = os.environ["TELEGRAM_BOT_TOKEN"]
chat_id = os.environ["TELEGRAM_CHAT_ID"]

url = f"https://api.telegram.org/bot{token}/sendMessage"
data = {
"chat_id": chat_id,
"text": text,
"parse_mode": "HTML"
}

resp = requests.post(url, data=data)
resp.raise_for_status()
return resp.json()["result"]["message_id"]

def update_message(message_id: int, text: str):
"""
Updates an existing message in the Telegram channel or group.
Returns True if successful, False if message not found.
"""
token = os.environ["TELEGRAM_BOT_TOKEN"]
chat_id = os.environ["TELEGRAM_CHAT_ID"]

url = f"https://api.telegram.org/bot{token}/editMessageText"
data = {
"chat_id": chat_id,
"message_id": message_id,
"text": text,
"parse_mode": "HTML"
}

try:
resp = requests.post(url, data=data)
resp.raise_for_status()
return True
except requests.exceptions.HTTPError as e:
if e.response.status_code == 400 and "message to edit not found" in e.response.text.lower():
return False
raise

def send_private_message(username: str, text: str):
"""
Sends a private message to a Telegram user using their username.
First gets the chat_id for the user, then sends the message.
Returns True if successful, False otherwise.
"""
token = os.environ["TELEGRAM_BOT_TOKEN"]

# First, try to get user info
url = f"https://api.telegram.org/bot{token}/getChat"
data = {
"chat_id": f"@{username.lstrip('@')}"
}

try:
# Get chat info
resp = requests.post(url, data=data)
resp.raise_for_status()
chat_data = resp.json()

if not chat_data.get("ok"):
print(f"Failed to get chat info for user @{username}: {chat_data.get('description')}")
return False

chat_id = chat_data["result"]["id"]

# Now send the message
url = f"https://api.telegram.org/bot{token}/sendMessage"
data = {
"chat_id": chat_id,
"text": text,
}

resp = requests.post(url, data=data)
resp.raise_for_status()
return True

except Exception as e:
print(f"Failed to send private message to @{username}: {str(e)}")
return False
5 changes: 2 additions & 3 deletions .github/ACDbot/modules/transcript.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import json
from modules import zoom, discourse
from modules import zoom, discourse, tg
import requests

MAPPING_FILE = ".github/ACDbot/meeting_topic_mapping.json"
Expand Down Expand Up @@ -103,8 +103,7 @@ def post_zoom_transcript_to_discourse(meeting_id: str):

# Now, send the same content to Telegram
try:
import modules.telegram as telegram # Ensure telegram module is available
telegram.send_message(post_content)
tg.send_message(post_content)
print("Message sent to Telegram successfully.")
except Exception as e:
print(f"Error sending message to Telegram: {e}")
Expand Down
2 changes: 1 addition & 1 deletion .github/ACDbot/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pytest==8.3.4
pytz==2022.1
Requests==2.32.3
python-dateutil>=2.8.2

python-telegram-bot==20.3



Loading

0 comments on commit 95c268d

Please sign in to comment.