-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.py
More file actions
78 lines (70 loc) · 2.67 KB
/
Copy pathsend.py
File metadata and controls
78 lines (70 loc) · 2.67 KB
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
import requests
import json
import logging
import os
# Configurar logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# Base URL of the API
BASE_URL = "http://127.0.0.1:8080/api"
def login(username, password):
"""Log in and get an authentication token."""
url = f"{BASE_URL}/login"
data = {
"username": username,
"password": password,
"peerId": "your_peer_id",
"photo": "your_photo_url"
}
try:
logging.info(f"Attempting to login with username: {username}")
response = requests.post(url, json=data)
response.raise_for_status()
token = response.json().get("token")
if not token:
raise ValueError("Token not found in response")
logging.info("Login successful")
return token
except requests.exceptions.RequestException as e:
logging.error(f"Login request failed: {e}")
logging.error(f"Response status code: {response.status_code}")
logging.error(f"Response content: {response.text}")
raise Exception("Login failed")
def send_message(token, title, message, subtitle="Sistema"):
"""Send a message using the API."""
url = f"{BASE_URL}/send"
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
params = {
"title": title,
"message": message,
"subtitle": subtitle
}
try:
logging.info(f"Attempting to send message: {message}")
response = requests.post(url, headers=headers, params=params)
response.raise_for_status()
logging.info("Message sent successfully")
return response.json()
except requests.exceptions.RequestException as e:
logging.error(f"Send message request failed: {e}")
logging.error(f"Response status code: {response.status_code}")
logging.error(f"Response content: {response.text}")
raise Exception("Failed to send message")
def main():
username = os.environ.get("BOT_USERNAME")
password = os.environ.get("BOT_PASSWORD")
if not username or not password:
raise RuntimeError("BOT_USERNAME and BOT_PASSWORD environment variables are required")
try:
token = login(username, password)
logging.info(f"Obtained token: {token[:10]}...") # Log first 10 characters of token
title = "Fontanero"
message = "24H PEPE tlf: 654321456" # cambia esto es el mensaje
response = send_message(token, title, message)
logging.info(f"Message sent response: {response}")
except Exception as e:
logging.error(f"An error occurred: {e}")
if __name__ == "__main__":
main()