关于linebot消息API? [英] About linebot message API?

查看:112
本文介绍了关于linebot消息API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LineBot是一个实现线路僵尸系统的免费工具。链接如下:

LineBot



去年,我尝试使用Azure来实现一个行机器人(使用BOT API试用版),它运行良好。

但是上周,我收到了来自Line的邮件,它提到Line将停止此服务,并将此服务移至Message API。



我为我的linebot写了一个webhook fumction在Azure功能上。

但它不起作用。

如果我想向特定的人发送消息,则需要userId。

如果我无法从webhook函数获取userId,那将是一个问题。谢谢。



顺便说一句,如果我先使用userId,推送功能就有效。



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

    // 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, "");
}







我试图用推送消息发回消息,但它显示userId。




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

    // 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.

推荐答案

Webhook被触发了!);

log.Verbose(
"Webhook was triggered!"); log.Verbose(


req: {REQ});
log.Verbose(
"req: {req}"); log.Verbose(


log:{log} );

string jsonContent = await req.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(jsonContent);

log.Verbose(
"log: {log}"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); log.Verbose(


这篇关于关于linebot消息API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆