c# - 我如何从 Telegram Bot 创建私有(private)消息?

标签 c# .net telegram telegram-bot telegram-webhook

我正在使用 webhook 连接到 Telegram Bot ,我想通过 Telegram 在私有(private)聊天中回复,但如果我发送 UID,它不会从机器人向用户发送任何消息。

这就是我所做的。

  1. 我使用 .net 框架创建了一个 Web API 项目,以通过 Telegram Bot 连接到 webhook。
  2. 作为用户,我编写了一个命令,该命令将返回一些对象列表。
  3. 我从 WebAPI 得到命令并正确处理
  4. 在发回响应时,我传递了这个 {"method":"sendMessage","chat_id":"[发送命令的用户的 UID]", "text":"[返回列表转换为字符串]", "reply_to_message_id “:[命令的消息 ID]”

这是我发送的实际代码

return new TelegramResponseModel 
{ method = "sendMessage", chat_id = newUpdate.message.chat.id.ToString(),
  text = text, reply_to_message_id = newUpdate.message.message_id };
  1. 在 Telegram 上没有任何反应!!

最佳答案

您可以使用 Nuget 包库来实现与名为 Telegram.Bot 的 Telegram 的集成。 .还有few examples如何使用这个库。 例如,这个简短的程序展示了如何使用 WebHook 的

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Owin.Hosting;
using Owin;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using File = System.IO.File;

namespace Telegram.Bot.Examples.WebHook
{
    public static class Bot
    {
        public static readonly TelegramBotClient Api = new TelegramBotClient("Your API Key");
    }

    public static class Program
    {
        public static void Main(string[] args)
        {
            // Endpoint must be configured with netsh:
            // netsh http add urlacl url=https://+:8443/ user=<username>
            // netsh http add sslcert ipport=0.0.0.0:8443 certhash=<cert thumbprint> appid=<random guid>

            using (WebApp.Start<Startup>("https://+:8443"))
            {
                // Register WebHook
                // You should replace {YourHostname} with your Internet accessible hosname
                Bot.Api.SetWebhookAsync("https://{YourHostname}:8443/WebHook").Wait();

                Console.WriteLine("Server Started");

                // Stop Server after <Enter>
                Console.ReadLine();

                // Unregister WebHook
                Bot.Api.DeleteWebhookAsync().Wait();
            }
        }
    }

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var configuration = new HttpConfiguration();

            configuration.Routes.MapHttpRoute("WebHook", "{controller}");

            app.UseWebApi(configuration);
        }
    }

    public class WebHookController : ApiController
    {
        public async Task<IHttpActionResult> Post(Update update)
        {
            var message = update.Message;

            Console.WriteLine("Received Message from {0}", message.Chat.Id);

            if (message.Type == MessageType.Text)
            {
                // Echo each Message
                await Bot.Api.SendTextMessageAsync(message.Chat.Id, message.Text);
            }
            else if (message.Type == MessageType.Photo)
            {
                // Download Photo
                var file = await Bot.Api.GetFileAsync(message.Photo.LastOrDefault()?.FileId);

                var filename = file.FileId + "." + file.FilePath.Split('.').Last();

                using (var saveImageStream = File.Open(filename, FileMode.Create))
                {
                    await Bot.Api.DownloadFileAsync(file.FilePath, saveImageStream);
                }

                await Bot.Api.SendTextMessageAsync(message.Chat.Id, "Thx for the Pics");
            }

            return Ok();
        }
    }
}

关于c# - 我如何从 Telegram Bot 创建私有(private)消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51745624/

相关文章:

c# - Windows Phone 8 AWAIT异步JSON

c# - 如何在没有用户密码的情况下获取 Alfresco 登录票证,但使用用户主体名称 (UPN) 模拟用户

c# - 当客户错过接受报价的机会时,应该使用什么 HTTP 响应代码

c# - 如何从字符串中提取子字符串 <img src ="myimage"/>?

c# - 将 asp.net core 从 .net 5 移植到 .net 6 时首页加载缓慢

.net - 有没有办法为 Visual Studio 中的引用库添加源代码浏览?

c# - 您对异常消息使用什么样式?

c# - 在 C# Telegram Bots 中使用键盘编辑短信

python - 有没有办法知道使用 telethon 的 telegram post 照片的 url?

我不参与的 Telegram API : How do I get messages from a public channel,?