본문 바로가기
프로그래밍/Node.js

Nodejs + google webhook 연동

by 뽀도 2023. 7. 5.

[google chat에 스페이스 만들기]

 

1. google chat에 스페이스 생성 

2. 생성한 스페이스 '앱 및 통합'에 들어가서 '웹 훅 추가' 하고 url 복사 

 

[프로그래밍하기]

 

1. node-fetch 모듈 인스톨 설치 

 

2. js 코드 작성

 - webhookURL 에 위에서 얻은 url 복붙 


function webhook(message) {

  const fetch = require('node-fetch');

  //web hook 주소
  const webhookURL = '';


  const data = JSON.stringify({
     'text': message,
  });


  let resp;

  fetch(webhookURL, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: data,
  }).then((response) => {
    resp = response;
    console.log(response);
  });

  return resp;
 
}

webhook("오늘 저녁 김밥, 떡볶이, 순대 어때?");

 

▶ webhook() 함수 호출하면서 메세지 수신 확인 

 

 

 

※ 에러 발생 후 정상 동작하지 않을때 아래의 포스트 참고 ↓

 

https://podo1017.tistory.com/351

반응형

'프로그래밍 > Node.js' 카테고리의 다른 글

Swagger를 적용해보자!  (0) 2023.12.21
nvm  (1) 2023.12.05
google chat - node.js 연동중 에러 발생  (0) 2023.07.04
[node.js] msgpack5 사용 예  (0) 2023.05.25
[WEB-HTTP] GET / POST / PUT / DELETE  (0) 2018.07.15

댓글