Фразу «Множественная доставка сообщения» можно интерпретировать как отправку сообщения нескольким получателям с использованием различных методов. Вот несколько методов, которые вы можете использовать, а также примеры кода:
- Электронная почта.
Вы можете отправить сообщение нескольким получателям с помощью службы электронной почты или библиотеки. Вот пример использования библиотеки Pythonsmtplib:
import smtplib
from email.mime.text import MIMEText
def send_email(subject, message, recipients):
# Create a MIMEText object with the message
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = 'sender@example.com'
msg['To'] = ', '.join(recipients)
# Connect to the SMTP server and send the email
with smtplib.SMTP('smtp.example.com', 587) as smtp:
smtp.login('username', 'password')
smtp.send_message(msg)
# Usage example
subject = 'Hello'
message = 'This is a test email'
recipients = ['recipient1@example.com', 'recipient2@example.com']
send_email(subject, message, recipients)
- SMS.
Вы можете использовать поставщика услуг SMS для отправки сообщений нескольким получателям. Вот пример использования Twilio API в Python:
from twilio.rest import Client
def send_sms(message, recipients):
account_sid = 'your_account_sid'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)
for recipient in recipients:
message = client.messages.create(
body=message,
from_='+1234567890', # Your Twilio phone number
to=recipient
)
# Usage example
message = 'This is a test SMS'
recipients = ['+1234567890', '+9876543210']
send_sms(message, recipients)
- Обмен мгновенными сообщениями.
Вы можете использовать платформы обмена мгновенными сообщениями для отправки сообщений нескольким получателям. Вот пример использования Telegram Bot API на Python:
import requests
def send_telegram_message(message, chat_ids):
bot_token = 'your_bot_token'
for chat_id in chat_ids:
url = f'https://api.telegram.org/bot{bot_token}/sendMessage'
params = {'chat_id': chat_id, 'text': message}
response = requests.post(url, params=params)
# Usage example
message = 'This is a test message'
chat_ids = ['chat_id1', 'chat_id2']
send_telegram_message(message, chat_ids)
- Push-уведомления.
Вы можете отправлять push-уведомления на несколько устройств с помощью служб push-уведомлений. Вот пример использования API OneSignal на Python:
import requests
def send_push_notification(message, player_ids):
app_id = 'your_app_id'
rest_api_key = 'your_rest_api_key'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Basic {rest_api_key}'
}
data = {
'app_id': app_id,
'contents': {'en': message},
'include_player_ids': player_ids
}
response = requests.post('https://onesignal.com/api/v1/notifications', headers=headers, json=data)
# Usage example
message = 'This is a test push notification'
player_ids = ['player_id1', 'player_id2']
send_push_notification(message, player_ids)