我如何从电报机器人创建私人消息?
How can i create a private message from telegram bot?
我正在使用 webhook 连接到电报机器人,我想通过电报在私人聊天中回复,但如果我发送 UID,它不会从机器人向用户发送任何消息。
这就是我所做的。
- 我使用 .net 框架创建了一个 Web API 项目,以使用电报机器人连接到 webhook。
- 作为用户,我编写了一个命令,该命令将 return 一些对象列表。
- 来自网络API 我收到命令并正确处理
- 在发回响应时我传递了这个 {"method":"sendMessage","chat_id":[发送命令的用户的 UID]", "text":" returning 列表转换为字符串]", "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 };
- 在电报上没有任何反应!!
您可以使用 Nuget 包库实现与名为 Telegram.Bot. Also there is few examples 的 Telegram 的集成。如何使用此库。
例如,这个简短的程序展示了如何使用 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();
}
}
}
这里有 php 上的功能代码:
<?php
$token = 'yout_boot_tocken';
$website = 'https://api.telegram.org/bot'.$token;
$input = file_get_contents('php://input');
$update = json_decode($input, TRUE);
$chatId = $update['message']['chat']['id'];
$message = $update['message']['text'];
$messageCode = strtoupper($message);
switch($messageCode) {
case 'hello':
$response = 'Hello my friend... how are you?';
sendMessage($chatId, $response);
break;
case 'address':
$response = 'Your Addres is Rosent wallet 245';
sendMessage($chatId, $response);
break;
case '/INFO':
$response = 'Hi, i am a Boot';
sendMessage($chatId, $response);
break;
case 'bye':
$response = 'it was a pleasure chat with you';
sendMessage($chatId, $response);
break;
default:
$response = 'I dont understand what do you mean with '.$messageCode;
sendMessage($chatId, $response);
break;
}
function sendMessage($chatId, $response) {
$url = $GLOBALS['website'].'/sendMessage?
chat_id='.$chatId.'&parse_mode=HTML&text='.urlencode($response);
file_get_contents($url);
}
?>
我正在使用 webhook 连接到电报机器人,我想通过电报在私人聊天中回复,但如果我发送 UID,它不会从机器人向用户发送任何消息。
这就是我所做的。
- 我使用 .net 框架创建了一个 Web API 项目,以使用电报机器人连接到 webhook。
- 作为用户,我编写了一个命令,该命令将 return 一些对象列表。
- 来自网络API 我收到命令并正确处理
- 在发回响应时我传递了这个 {"method":"sendMessage","chat_id":[发送命令的用户的 UID]", "text":" returning 列表转换为字符串]", "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 };
- 在电报上没有任何反应!!
您可以使用 Nuget 包库实现与名为 Telegram.Bot. Also there is few examples 的 Telegram 的集成。如何使用此库。 例如,这个简短的程序展示了如何使用 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();
}
}
}
这里有 php 上的功能代码:
<?php
$token = 'yout_boot_tocken';
$website = 'https://api.telegram.org/bot'.$token;
$input = file_get_contents('php://input');
$update = json_decode($input, TRUE);
$chatId = $update['message']['chat']['id'];
$message = $update['message']['text'];
$messageCode = strtoupper($message);
switch($messageCode) {
case 'hello':
$response = 'Hello my friend... how are you?';
sendMessage($chatId, $response);
break;
case 'address':
$response = 'Your Addres is Rosent wallet 245';
sendMessage($chatId, $response);
break;
case '/INFO':
$response = 'Hi, i am a Boot';
sendMessage($chatId, $response);
break;
case 'bye':
$response = 'it was a pleasure chat with you';
sendMessage($chatId, $response);
break;
default:
$response = 'I dont understand what do you mean with '.$messageCode;
sendMessage($chatId, $response);
break;
}
function sendMessage($chatId, $response) {
$url = $GLOBALS['website'].'/sendMessage?
chat_id='.$chatId.'&parse_mode=HTML&text='.urlencode($response);
file_get_contents($url);
}
?>