-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineAPI.csx
124 lines (108 loc) · 3.51 KB
/
LineAPI.csx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
/// <summary>
/// Lineからコンテンツを取得
/// </summary>
/// <returns>Stream</returns>
static async Task<Stream> GetLineContents(string messageId)
{
Stream responsestream = new MemoryStream();
// 画像を取得するLine APIを実行
using (var getContentsClient = new HttpClient())
{
// 認証ヘッダーを追加
getContentsClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {Environment.GetEnvironmentVariable("LINE_CHANNEL_ACCESS_TOKEN")}");
// 非同期でPOST
var res = await getContentsClient.GetAsync($"https://api-data.line.me/v2/bot/message/{messageId}/content");
responsestream = await res.Content.ReadAsStreamAsync();
}
return responsestream;
}
// <summary>
/// Lineににreplyを送信する
/// </summary>
/// <returns>Stream</returns>
static async Task PutLineReply(Response content, TraceWriter log)
{
// JSON形式に変換
var reqData = JsonConvert.SerializeObject(content);
// レスポンスの作成
using (var client = new HttpClient())
{
// リクエストデータを作成
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://api.line.me/v2/bot/message/reply");
request.Content = new StringContent(reqData, Encoding.UTF8, "application/json");
// 認証ヘッダーを追加
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {Environment.GetEnvironmentVariable("LINE_CHANNEL_ACCESS_TOKEN")}");
// 非同期でPOST
var res = await client.SendAsync(request);
log.Info($"{res}");
}
}
/// <summary>
/// リプライ情報の作成
/// </summary>
/// <param name="token"></param>
/// <param name="translateWord"></param>
/// <param name="log"></param>
/// <returns>response</returns>
static Response CreateResponse(string token,string translateWord,TraceWriter log)
{
Response res = new Response();
Messages msg = new Messages();
// リプライトークンはリクエストに含まれるリプライトークンを使う
res.replyToken = token;
res.messages = new List<Messages>();
// メッセージタイプがtext以外は単一のレスポンス情報とする
msg.type = "text";
msg.text = translateWord;
res.messages.Add(msg);
return res;
}
// ******************************************************
// リクエスト
public class Request
{
public List<Event> events { get; set; }
}
public class Event
{
public string replyToken { get; set; }
public string type { get; set; }
public object timestamp { get; set; }
public Source source { get; set; }
public message message { get; set; }
}
public class Source
{
public string type { get; set; }
public string userId { get; set; }
}
public class message
{
public string id { get; set; }
public string type { get; set; }
public string text { get; set; }
}
// ******************************************************
// ******************************************************
// レスポンス
public class Response
{
public string replyToken { get; set; }
public List<Messages> messages { get; set; }
}
// レスポンスメッセージ
public class Messages
{
public string type { get; set; }
public string text { get; set; }
}
// ******************************************************