파이썬 통합
내장된 urllib 또는 널리 사용되는 requests 라이브러리를 사용하면 SMSBAT API를 Python 애플리케이션에 간단하게 통합할 수 있습니다.
요청 사용(권장)
먼저 'requests' 라이브러리를 설치하세요:
그런 다음 다음 코드를 사용하여 메시지를 보냅니다.
import requests
import json
url = 'https://api.smsbat.com/bat/messagelist'
api_key = 'YOUR_API_KEY_HERE'
headers = {
'Content-Type': 'application/json',
'X-Authorization-Key': api_key
}
payload = {
"messages": [
{
"from": "ALPHANAME",
"to": "380501234567",
"text": "Hello from Python and SMSBAT!",
"type": "sms"
}
]
}
try:
response = requests.post(url, headers=headers, json=payload, timeout=10)
response.raise_for_status() # Raise an exception for HTTP errors
print(f"Status Code: {response.status_code}")
print(f"Response: {response.json()}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
if hasattr(e, 'response') and e.response is not None:
print(f"Error Details: {e.response.text}")
aiohttp 사용(비동기)
비동기 애플리케이션을 구축하는 경우(예: FastAPI 또는 asyncio 사용) aiohttp를 사용할 수 있습니다.
import aiohttp
import asyncio
async def send_message():
url = 'https://api.smsbat.com/bat/messagelist'
api_key = 'YOUR_API_KEY_HERE'
headers = {
'Content-Type': 'application/json',
'X-Authorization-Key': api_key
}
payload = {
"messages": [
{
"from": "ALPHANAME",
"to": "380501234567",
"text": "Hello from Async Python!",
"type": "sms"
}
]
}
async with aiohttp.ClientSession() as session:
try:
async with session.post(url, headers=headers, json=payload) as response:
print(f"Status Code: {response.status}")
result = await response.json()
print(f"Response: {result}")
except Exception as e:
print(f"An error occurred: {e}")
# Run the async function
if __name__ == '__main__':
asyncio.run(send_message())