Click here to Skip to main content
15,889,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
#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}");       

    // get jsonContent information.
    // reply userid
   
    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");


    // do a post to line service
                  
    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 + "\"to\": \"xxxxxxxxxxxxxxxxxxxxxxxxxxx\",";
        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".
C#
#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}");       

    // get jsonContent information.
    // reply userid
   
    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");


    // do a post to line service   
                  
    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 + "\"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, "");
}


What I have tried:

Information about this function.
Posted
Updated 17-Nov-16 16:56pm
v10
Comments
Richard MacCutchan 10-Nov-16 5:46am    
Contact the people who sent you the message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900