-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
65 lines (55 loc) · 2.27 KB
/
bot.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
import os
import json
import httplib2
import google.auth
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient import errors, discovery
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
# Google Sheets API setup
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
SHEET_ID = '1torhO_gjLKIYcFHe7c7PVszn8kttI5pIcl_ZlUHs7tk'
RANGE_NAME = 'Foglio1!A1:B3' # Update according to your sheet range
def get_google_sheets_credentials():
creds = None
if os.path.exists('token.json'):
with open('token.json', 'r') as token:
creds = google.oauth2.credentials.Credentials.from_authorized_user_info(info=json.load(token))
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
return creds
def read_data_from_google_sheet(sheet_id, range_name):
creds = get_google_sheets_credentials()
service = discovery.build('sheets', 'v4', credentials=creds)
try:
result = service.spreadsheets().values().get(spreadsheetId=sheet_id, range=range_name).execute()
rows = result.get('values', [])
return rows
except errors.HttpError as error:
print(f"An error occurred: {error}")
return None
def post_message_to_slack(channel, text):
slack_token = os.environ["SLACK_API_TOKEN"]
client = WebClient(token=slack_token)
try:
response = client.chat_postMessage(channel=channel, text=text)
print(f"Message posted: {text}")
except SlackApiError as e:
print(f"Error posting message: {e}")
if __name__ == '__main__':
sheet_data = read_data_from_google_sheet(SHEET_ID, RANGE_NAME)
if sheet_data:
message = "Daily update:\n"
for row in sheet_data:
message += f"{row[0]}: {row[1]}\n"
slack_channel = "#status" # Update with your desired Slack channel
post_message_to_slack(slack_channel, message)
else:
print("No data found.")