Re: Convert Python Code to Apps Script (API)

145 views
Skip to first unread message
Message has been deleted

홍현화[HyunHwa Hong]

unread,
Mar 8, 2023, 12:40:03 AM3/8/23
to Google Apps Script Community
function unique_id() {
  return Utilities.getUuid();
}

function get_iso_datetime() {
  var offset = new Date().getTimezoneOffset();
  var date = new Date(Date.now() - offset * 60 * 1000).toISOString();
  return date;
}

function get_signature(key, msg) {
  var hmacDigest = Utilities.computeHmacSha256Signature(msg, key);
  var signature = Utilities.base64Encode(hmacDigest);
  return signature;
}

function get_headers(apiKey, apiSecret) {
  var date = get_iso_datetime();
  var salt = unique_id();
  var data = date + salt;
  return {
    'Authorization': 'HMAC-SHA256 ApiKey=' + apiKey + ', Date=' + date + ', salt=' + salt + ', signature=' + get_signature(apiSecret, data),
    'Content-Type': 'application/json; charset=utf-8'
  };
}

function getUrl(path) {
  var url = protocol + '://' + domain;
  if (prefix !== '') {
    url += prefix;
  }
  url += path;
  return url;
}

function sendMany(data) {
  var options = {
    'method': 'post',
    'headers': get_headers(apiKey, apiSecret),
    'contentType': 'application/json',
    'payload': JSON.stringify(data)
  };
  return UrlFetchApp.fetch(getUrl('/messages/v4/send-many'), options);
}

function main() {
  var data = {
    'messages': [
      {
        'to': '01000000000',
        'from': '01000000001',
        'text': 'test'
      }
    ]
  };
  var res = sendMany(data);
  Logger.log(res.getContentText());
}


2023년 3월 8일 수요일 오후 2시 33분 52초 UTC+9에 어썸포유_Awesome4U님이 작성:
import json
import time
import datetime
import uuid
import hmac
import hashlib
import requests
# apiKey, apiSecret 입력 필수
apiKey = 'xxxxx'
apiSecret = 'xxxxx'
# 아래 값은 필요시 수정
protocol = 'https'
domain = 'api.solapi.com'
prefix = ''
def unique_id():
    return str(uuid.uuid1().hex)
def get_iso_datetime():
    utc_offset_sec = time.altzone if time.localtime().tm_isdst else time.timezone
    utc_offset = datetime.timedelta(seconds=-utc_offset_sec)
    return datetime.datetime.now().replace(tzinfo=datetime.timezone(offset=utc_offset)).isoformat()
def get_signature(key, msg):
    return hmac.new(key.encode(), msg.encode(), hashlib.sha256).hexdigest()
def get_headers(apiKey, apiSecret):
    date = get_iso_datetime()
    salt = unique_id()
    data = date + salt
    return {
      'Authorization': 'HMAC-SHA256 ApiKey=' + apiKey + ', Date=' + date + ', salt=' + salt + ', signature=' +
                             get_signature(apiSecret, data),
      'Content-Type': 'application/json; charset=utf-8'
    }
def getUrl(path):
  url = '%s://%s' % (protocol, domain)
  if prefix != '':
    url = url + prefix
  url = url + path
  return url
def sendMany(data):
  return requests.post(getUrl('/messages/v4/send-many'), headers=get_headers(apiKey, apiSecret), json=data)
# 내
if __name__ == '__main__':
  data = {
    'messages': [
      {
        'to': '01000000000',
        'from': '01000000001',
        'text': 'test'
      }
          ]
        }

  res = sendMany(data)
  print(json.dumps(res.json(), indent=2, ensure_ascii=False))


I want to convert Python code to Apps script, but I can't. Help me.

Netmarble Confidential

이 메일은 지정된 수취인만을 위해 작성되었으며, 중요한 정보나 저작권을 포함하고 있을 수 있습니다. 어떠한 권한 없이, 본 문서에 포함된 정보의 전부 또는 일부를 무단으로 제3자에게 공개, 배포, 복사 또는 사용하는 것을 엄격히 금지합니다. 만약, 본 메일이 잘못 전송된 경우, 발신인 또는 당사에 알려주시고, 본 메일을 즉시 삭제하여 주시기 바랍니다.
This E-mail may contain confidential information and/or copyright material. This email is intended for the use of the addressee only. If you receive this email by mistake, please either delete it without reproducing, distributing or retaining copies thereof or notify the sender immediately.

Reply all
Reply to author
Forward
0 new messages