LineBot Note
namespace WebApplication17.Controllers
{
public class LineChatController : ApiController
{
public string ChannelAccessToken = @"AI....";
public string token = = @"AI....";
/// <summary>
/// v3_回覆圖型與貼圖
/// 貼圖 : Hhttps://devdocs.line.me/files/sticker_list.pdf
/// </summary>
/// <returns></returns>
[HttpPost]
public IHttpActionResult POST()
{
isRock.LineBot.Bot bot;
bot = new isRock.LineBot.Bot(ChannelAccessToken);
try
{
//取得 http Post RawData(should be JSON)
string postData = Request.Content.ReadAsStringAsync().Result;
//剖析JSON
var ReceivedMessage = isRock.LineBot.Utility.Parsing(postData);
var UserSays = ReceivedMessage.events[0].message.text;
var ReplyToken = ReceivedMessage.events[0].replyToken;
// USER ID
var userInfo = bot.GetUserInfo(ReceivedMessage.events.FirstOrDefault().source.userId);
//依照用戶說的特定關鍵字來回應
switch (UserSays.ToLower())
{
case "1":
//回覆貼圖
bot.ReplyMessage(ReplyToken, 1, 11);
break;
case "2":
//回覆圖片
bot.ReplyMessage(ReplyToken, new Uri("https://external-tpe1-1.xx.fbcdn.net/safe_image.php?d=AQAc_b9uFr5VR0cg&w=476&h=249&url=fbstaging%3A%2F%2Fgraph.facebook.com%2Fstaging_resources%2FMDExMzQ5NDA3ODM1MDk4Nzg2OjI2MDY5NTAzMQ%3D%3D&cfs=1&upscale=1&_nc_hash=AQDJXtISk8IViKtg"));
break;
default:
//回覆訊息
string Message = "哈囉,"+ userInfo.displayName + " 你說了:" + UserSays;
//回覆用戶
bot.ReplyMessage(ReplyToken, Message);
break;
}
//回覆API OK
return Ok();
}
catch (Exception ex)
{
string msg = ex.Message;
return Ok();
}
}
/// <summary>
/// Copy User said_V2
/// </summary>
///// <returns></returns>
/*
[HttpPost]
public IHttpActionResult POST()
{
try
{
//取得 http Post RawData(should be JSON)
string postData = Request.Content.ReadAsStringAsync().Result;
//剖析JSON
var ReceivedMessage = isRock.LineBot.Utility.Parsing(postData);
//回覆訊息
string Message;
Message = "你說了:" + ReceivedMessage.events[0].message.text;
//回覆用戶
isRock.LineBot.Utility.ReplyMessage(ReceivedMessage.events[0].replyToken, Message, ChannelAccessToken);
//回覆API OK
return Ok();
}
catch (Exception ex)
{
return Ok();
}
}
*/
/// <summary>
/// DB_V1
/// </summary>
///// <returns></returns>
/*
[HttpPost]
public IHttpActionResult POST()
{
try
{
//取得資料
string postData = Request.Content.ReadAsStringAsync().Result;
//解析-取得資料
var ReceivedMessage = isRock.LineBot.Utility.Parsing(postData);
//使用這輸入
string UserInput = ReceivedMessage.events[0].message.text;
//回覆訊息
string ReplyUserMsg = "";
string ReplyToken = ReceivedMessage.events[0].replyToken;
//加入SQL
///////////////////////////////////////////////////////////////////////////////////
SqlDataAdapter apt;
DataTable dt;
using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["JudyCon"].ConnectionString.ToString()))
{
String sql = @"SELECT ......";
SqlCommand cmd = new SqlCommand(sql, con);
SetCommandParam("@fun_code", UserInput, cmd);
apt = new SqlDataAdapter(sql, con);
apt.SelectCommand = cmd;
dt = new DataTable();
apt.Fill(dt);
}
if (dt.Rows.Count == 0)
{
ReplyUserMsg = "查無資料";
}
else
{
string a= ""; //
string b= ""; //
foreach (DataRow dr in dt.Rows)
{
a= dr["a"].ToString();
b= dr["b"].ToString();
ReplyUserMsg = a+ "_" + b;
}
}
//回覆用戶
isRock.LineBot.Utility.ReplyMessage(ReplyToken, ReplyUserMsg, ChannelAccessToken);
///////////////////////////////////////////////////////////////////////////////////
//呼叫回覆訊息的API
return Ok();
}
catch (Exception ex)
{
return Ok();
}
}
*/
public void SetCommandParam(string ParamName, object ParamValue, SqlCommand cmd)
{
if (cmd.Parameters.IndexOf(ParamName) == -1)
{
cmd.Parameters.AddWithValue(ParamName, ParamValue);
}
else
{
cmd.Parameters[ParamName].Value = ParamValue;
}
}
}
}
var UserID = isRock.LineBot.Utility.Parsing(postData).events[0].source.userId;
(LINE 身分證?)