问题描述
我正在我现有的 asp.net core 3.1 项目中开发电报通知。我在控制器中编写了以下代码。
#region Telegram
TelegramBotClient _botService;
private const string token = "332435:45345345345dflskdfjksdjskdjflkdd";
[HttpPost("Update")]
[AllowAnonymous]
public async Task<IActionResult> Update([FromBody] Update update)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
if (_botService == null)
_botService = new TelegramBotClient(token);
if (update.Type != UpdateType.Message)
return Ok(new Response
{
code = (int)HttpStatusCode.OK,status = "Ok",message = "Success"
});
var message = update.Message;
try
{
_logger.Loginformation("Received Message from {0}",message.Chat.Id);
switch (message.Type)
{
case MessageType.Text:
if (message.Text.Contains("/Reset"))
{
//Delete(string chatid)
var response = _UserRepository.DeleteTeleBotChatID(message.Chat.Id);
if (response)
await _botService.SendTextMessageAsync(message.Chat.Id,"You have successfully unsubscribed.");
else
await _botService.SendTextMessageAsync(message.Chat.Id,"You are not registered yet.");
}
else
if (message.Text.Contains("/") && !message.Text.ToLower().Contains("/start"))
{
var user = Crypto.decrypt(Encoding.UTF8.GetString(Convert.FromBase64String(message.Text.Split('/').Last())));
var response = _UserRepository.UpdateTeleBotChatIDByUser(new TeleBotModel() { ChatId = message.Chat.Id,Username = user });
if (response)
await _botService.SendTextMessageAsync(message.Chat.Id,$"You have successfully subscribe notifications for {user}.");
else
await _botService.SendTextMessageAsync(message.Chat.Id,"Username is not valid");
// var chat=modifyus(string username,chatid)
}
else
{
await _botService.SendTextMessageAsync(message.Chat.Id,"Enter your encrypted username.\n Type /Reset to unsubscribe.");
}
break;
case MessageType.Photo:
// Download Photo
var fileId = message.Photo.LastOrDefault()?.FileId;
var file = await _botService.GetFileAsync(fileId);
var filename = file.FileId + "." + file.FilePath.Split('.').Last();
using (var saveimagestream = System.IO.File.Open(filename,FileMode.Create))
{
await _botService.DownloadFileAsync(file.FilePath,saveimagestream);
}
await _botService.SendTextMessageAsync(message.Chat.Id,"Thx for the Pics");
break;
}
}
catch (Exception exp)
{
//LoggerSimple.Error(exp);
await _botService.SendTextMessageAsync(message.Chat.Id,"Wrong Bot command");
}
return Ok(new Response
{
code = (int)HttpStatusCode.OK,message = "Success"
});
}
[HttpPost]
public async Task<IActionResult> sendTeleMsg(TelegramMessgae Data)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
if (_botService == null)
_botService = new TelegramBotClient(token);
//check username exist
long ChatId = _UserRepository.GetChatIdByUsername(Data.Username);
if (ChatId == -1)
{
return Ok(new Response
{
error = "true",code = HttpStatusCode.BadRequest,status = HttpStatus.OK,message = "Not registered with telegram bot"
});
}
try
{
await _botService.SendTextMessageAsync(ChatId,string.Format("*{0}*\n{1}",parseMText(Data.Subject),parseMText(Data.Message)),ParseMode.Markdown);
return Ok(new Response
{
code = HttpStatusCode.OK,message = "Message Sent"
});
}
catch (Exception exp)
{
//if wrong chatid
_UserRepository.DeleteTeleBotChatID(ChatId);
return Ok(new Response
{
error = "true",message = exp.Message
});
}
}
private string parseMText(string txt)
{
var vs = new string[] { "*","_","`","[","]" };
foreach (var item in vs)
{
txt = txt.Replace(item,"\\" + item);
}
return txt;
}
#endregion
然后使用 ngrok 进行隧道传输并公开本地主机,以便我可以与电报机器人连接。创建并订阅机器人后,我可以在上述 Update 方法中收到断点,但数据什么也没有。我在机器人上发送了消息,但更新对象中总是没有数据。请看下面的截图。 我无法弄清楚代码中的问题。任何人都可以帮忙吗?