C#으로 라인 메시지 보내기 샘플입니다.

 

*참고 : python으로 line notify를 사용했던 샘플입니다. 필요하시면 확인해주세요.

https://vmpo.tistory.com/87

 

python으로 Line notify (라인 메시지) 보내기

파이썬 및 Line notify를 활용해 라인 그룹 채팅방에 메시지를 보내보도록 하겠습니다. 먼저, 파이썬은 기본적으로 설치 되어 있다고 가정하겠습니다. 만약 설치되어 있지 않다면, 아래 링크를 확인해주세요 https..

vmpo.tistory.com

 

 

python예제와 거의 동일하며 코드실행만 C#으로 진행하시면 됩니다.

 

라인앱을 설치합니다.

처음 가입하신 경우 이메일, 비밀번호를 등록해줍니다.

완료되었으면,

대화방으로 이동해 그룹채팅방을 하나 만들어 줍니다.

 

 

친구초대를 눌러 line notify를 검색해서 추가해줍니다.

 

완료되었으면,

Line notify 홈페이지에서 로그인을 진행해줍니다.

 

 

우측 상단의 자신의 아이디를 누르고 mypage로 이동합니다.

그리고, mypage 하단에 generate access token 메뉴에서

Generate token을 클릭합니다.

 

아까 만들었던 그룹 채팅방을 선택해줍니다.

 

 

Generate token을 눌러주면 아래와 같이 토큰이 생성됩니다.

이 토큰은 다시는 찾을 수 없으니 저장을 따로 해줍니다.

 

아래와 같이 생성되었다면 성공입니다.

 

 

 

 

그럼 이제 C#코드로 메시지를 날려보겠습니다.

코드는 아래와 같습니다.

헤더값중 authorization 에 Bearer '토큰값' 만 설정해주면 됩니다.

전송 파라미터는 message : '전송할 메시지'만 설정해 주면 됩니다.

그럼 실제 코드를 실행해 보겠습니다.

using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;

namespace LineNotify
{
    class Program
    {
        static void Main(string[] args)
        {

            try
            {
                WebClient wc4 = new WebClient();
                string targetAddress4 = "https://notify-api.line.me/api/notify";
                wc4.Headers["Authorization"] = "Bearer 1EUq0iNdBGAVGx4Jm9mUvcSLmfvgV61OH1";
                
                NameValueCollection nc4 = new NameValueCollection();
                nc4["message"] = "안녕하세요~~";

                byte[] bResult4 = wc4.UploadValues(targetAddress4, nc4);
                string result4 = Encoding.UTF8.GetString(bResult4);
            }
            catch(Exception e)
            {

            }            
        }
    }
}

 

아래와 같이 정상적으로 호출되는 것을 확인 할 수 있습니다.

비동기 코드가 필요하신 경우 아래와 같이

HttpClient 클래스를 활용해서 호출 할 수 있습니다. ( 닷넷 프레임워크 4.5 이상만 가능)

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace LineNotify
{
    class Program
    {
        static async Task Main(string[] args)
        {
            await GetAsyncHttp();            
        }

        public static async Task GetAsyncHttp()
        {
            HttpClient _httpClient = new HttpClient();
            _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer BGAVGx4Jm9mUvcSLmfnY");

            var parameters = new Dictionary<string, string>();
            parameters.Add("message", "안녕하세요");
            var encodedContent = new FormUrlEncodedContent(parameters);

            var response = await _httpClient.PostAsync("https://notify-api.line.me/api/notify", encodedContent).ConfigureAwait(false);
        }
    }
}

 

 

LIST
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기