MT4からLINEに通知を送る

# はじめに
MT4からLINEに通知を送る仕組みを作成していきます。


# LINE Notify終了
LINE Notifyが2025年3月31日にサービスが終了します。

https://notify-bot.line.me/closing-announce

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/bb964b1e-755f-f4b8-fffd-b6c1ca2572e0.png)

LINE Notifyの代替方法として、LINE公式アカウントから
メッセージを送信できるMessaging APIを使ってみます。

# LINE messaging API
LINE Messaging APIは、LINEのプラットフォームを活用して
自動化されたメッセージ送受信や対話型のチャット機能を提供するためのAPIです。
これを使用すると、LINE上でチャットボットや通知システムを構築したり、
LINEを通じてさまざまなサービスを統合することができます。

https://developers.line.biz/ja/docs/messaging-api/overview/


# 手順

# LINE Developers
https://developers.line.biz/ja/

「新規プロバイダー作成」をクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/59805927-dd84-9b56-495b-283513cc3c39.png)

適当にプロバイダー名を命名し新規プロバイダーを作成
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/d5ab01b7-14a6-f7a0-68e6-d6a6edbc5d45.png)

「Messaging API」をクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/7369a44b-5fb5-6611-8864-79dde80be347.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/7275af8c-5760-d69c-08c9-11eff0adc17b.png)

「LINE公式アカウントの作成」
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/2848caf3-b7b5-8da8-a173-c3dc5655f605.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/aaa9242e-79a1-66f6-5cc3-5056932eb607.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/cdac05bb-ef48-0d38-6c76-f0398cc33532.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/aa254318-70df-f753-773b-6ffd022f6291.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/2e3008f4-e477-a4b9-8168-a8535e86a457.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/0a190e1a-7e48-f11f-4936-60f778e55e3a.png)



![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/61caf0fd-411a-c4dc-66bc-e8fb9ca67cfb.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/2122c906-6bc6-92b1-2c12-970ade7e468e.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/5f61fcef-af5f-6ca4-a015-16a739bce5b3.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/13006cea-b992-c67a-96c3-947e0672a542.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/2e7c74d6-a3f1-0d6a-2c7f-c53759463e26.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/09975a33-3493-5c62-ad12-42582eded4b5.png)



チャンネルアクセストークンを取得
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/cc94313d-b851-c4cb-1bd2-a078801a7c29.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/89e06126-baf1-15b4-8ce5-f498ac1c6dd5.png)

ユーザーIDを取得
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/a6efa21e-f151-49f2-18d8-94cb92a0dc99.png)



```shell
curl -v -X POST https://api.line.me/v2/bot/message/push \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {"access token"}' \
-d '{
    "to": "user_id",
    "messages":[
        {
            "type":"text",
            "text":"Hello, world1"
        },
        {
            "type":"text",
            "text":"Hello, world2"
        }
    ]
}'
```

curlコマンドをたたきます
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/d4b1e11c-c568-e309-84ea-483cc02f46cc.png)

LINEにメッセージが通知されることを確認します。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/8990b8f6-929b-c7a4-14cc-77bf0eb95192.png)



# MT4からLINEに通知
下記がMT4からLINEに通知を送るインジケータです。
wininet.dllを使っています。


```C++:tet_mt4_to_line.mq4
#property copyright ""
#property link      ""
#property version   "1.00"
#property strict
#property indicator_chart_window

#define OPEN_TYPE_PRECONFIG                0           // use the configuration by default
#define FLAG_KEEP_CONNECTION               0x00400000  // do not terminate the connection
#define FLAG_PRAGMA_NOCACHE                0x00000100  // no cashing of the page
#define FLAG_RELOAD                        0x80000000  // receive the page from the server when accessing it
#define SERVICE_HTTP                       3           // the required protocol
#define DEFAULT_HTTPS_PORT                 443
#define FLAG_SECURE                        0x00800000  // use PCT/SSL if applicable (HTTP)
#define INTERNET_FLAG_SECURE               0x00800000
#define INTERNET_FLAG_KEEP_CONNECTION      0x00400000
#define HTTP_ADDREQ_FLAG_REPLACE           0x80000000
#define HTTP_ADDREQ_FLAG_ADD               0x20000000

#import "wininet.dll"
int InternetOpenW(string sAgent,int lAccessType,string sProxyName,string sProxyBypass,int lFlags);
int InternetConnectW(int hInternet,string ServerName, int nServerPort,string lpszUsername, string lpszPassword, int dwService,int dwFlags,int dwContext);
int HttpOpenRequestW(int hConnect, string Verb, string ObjectName, string Version, string Referer, string AcceptTypes, int dwFlags, int dwContext);
int HttpSendRequestW(int hRequest, string &lpszHeaders, int dwHeadersLength, uchar &lpOptional[], int dwOptionalLength);
int InternetCloseHandle(int hInet);
#import


input string access_token = "accecc_token";
input string user_id = "User ID";

int OnInit()
{
  CallLineMessagingAPI("Hello World!!");
  return(INIT_SUCCEEDED);
}


int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
  return(rates_total);
}



void CallLineMessagingAPI(string message) {
  string sServerName = "api.line.me";
  string sObjectName = "/v2/bot/message/push";
  string headers = "";
  string dataString = "{ \"to\": \"" +user_id + "\", \"messages\":[ {\"type\": \"text\",\"text\": \"" + message + "\"} ] }";
  headers += "Content-Type: application/json\r\nAuthorization: Bearer {\" "+ access_token +   "\"}";
  uchar data[];
  StringToCharArray(dataString, data, 0, -1, CP_UTF8);
  int session = InternetOpenW("MetaTrader 4 Terminal", 0, "", "", 0);
  int connect = InternetConnectW(session, sServerName, DEFAULT_HTTPS_PORT, "", "", SERVICE_HTTP, 0, 0);
  int hRequest = HttpOpenRequestW(connect, "POST", sObjectName, "HTTP/1.1", NULL, NULL, (int)(FLAG_SECURE | FLAG_KEEP_CONNECTION | FLAG_RELOAD | FLAG_PRAGMA_NOCACHE), 0);
  int hSend = HttpSendRequestW(hRequest, headers, StringLen(headers), data, ArraySize(data) - 1);
  
  Closehandle(session, connect);
  InternetCloseHandle(hRequest);
  InternetCloseHandle(hSend);
}

void Closehandle(int session_, int connect_) {
  //+---------------------------------------------------+
  //|   セッションを閉じる関数
  //+---------------------------------------------------+

  if(session_ > 0){
    InternetCloseHandle(session_);
    session_ = -1;
  }
  if(connect_ > 0) {
    InternetCloseHandle(connect_);
    connect_ = -1;
  }
}
```

下記の箇所を自身の
アクセストークンとユーザIDに置き換えてください。
```C++
input string access_token = "accecc_token";
input string user_id = "User ID";
```

MT4に設置すると下記のようにLINEに通知が飛びます。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/711148/3c2e4846-322a-2505-cbcd-1256e2ced4be.png)