LineBot is a free tool to implement line bot system. The link as follow:
LineBot
Last year, I tried to use Azure to implement a line bot(with BOT API Trial), and it works very well.
But last week, I received a mail from Line, and it mentioned Line will stop this services , and move this service to "Message API".
I wrote a webhook fumction for my linebot on Azure function.
but it doesn't work.
If I want to send a message to a specific person, a userId is needed.
if I can't get userId from webhook function, it will be a issues. Thanks.
By the way, push function works if I have userId first.
#r "Newtonsoft.Json"
using System;
using System.Net;
using Newtonsoft.Json;
using System.Net;
using System.IO;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"Webhook was triggered!");
log.Verbose($"req: {req}");
log.Verbose($"log: {log}");
string jsonContent = await req.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(jsonContent);
log.Verbose($"jsonContent: {jsonContent}");
log.Info($"start loop");
string l_userId= "";
string l_replyToken= "";
using (JsonTextReader reader = new JsonTextReader(new StringReader(jsonContent)))
{
string l_value = "";
int l_flag = 0;
while (reader.Read())
{
try{
l_value = (string)reader.Value;
if (l_value.Equals("userId")){
reader.Read();
l_userId = l_value;
}
if (l_value.Equals("replyToken")){
reader.Read();
l_replyToken =l_value;
}
}
catch(Exception ex){
log.Verbose($"error: {ex.ToString()}");
}
}
log.Info($"in loop");
}
log.Info($"out loop");
string Url = "https://api.line.me/v2/bot/message/reply";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization", "Bearer " + "{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx}");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "";
json = json + "{";
json = json + "\"replyToken\":\"" + l_replyToken + "\",\n";
json = json + "\"messages\":[";
json = json + "{";
json = json + "\"type\":\"text\",";
json = json + "\"text\":\"" + l_userId + "\"\n";
json = json + "}";
json = json + "]";
json = json + "}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
return req.CreateResponse(HttpStatusCode.OK, "");
}
I tried to use push message to send a message back , but it shows "userId".
#r "Newtonsoft.Json"
using System;
using System.Net;
using Newtonsoft.Json;
using System.Net;
using System.IO;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"Webhook was triggered!");
log.Verbose($"req: {req}");
log.Verbose($"log: {log}");
string jsonContent = await req.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(jsonContent);
log.Verbose($"jsonContent: {jsonContent}");
log.Info($"start loop");
string l_userId= "";
string l_replyToken= "";
using (JsonTextReader reader = new JsonTextReader(new StringReader(jsonContent)))
{
string l_value = "";
int l_flag = 0;
while (reader.Read())
{
try{
l_value = (string)reader.Value;
if (l_value.Equals("userId")){
reader.Read();
l_userId = l_value;
}
if (l_value.Equals("replyToken")){
reader.Read();
l_replyToken =l_value;
}
}
catch(Exception ex){
log.Verbose($"error: {ex.ToString()}");
}
}
log.Info($"in loop");
}
log.Info($"out loop");
string Url = "https://api.line.me/v2/bot/message/push";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization", "Bearer " + "{xxxxxxxxxxxxxxxxxxxxxxx}");
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "";
json = json + "{";
json = json + "\"to\": \"xxxxxxxxxxxxxxxxxxxx\",";
json = json + "\"messages\":[";
json = json + "{";
json = json + "\"type\":\"text\",";
json = json + "\"text\":\"" + l_userId + "\"\n";
json = json + "}";
json = json + "]";
json = json + "}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
return req.CreateResponse(HttpStatusCode.OK, "");
}
What I have tried:
Information about this function.