I want to send a message and photo to a channel with my bot in c# 2013. Message and photo should be sent in one box. Photo in above the message .
i can send a message to the channel successfully but there are 2 problems:
- when send photo , this error shows:
The remote server returned an error: (400) Bad Request.
- I cannot send a text and photo together in only 1 send.
code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telegram.Bot;
using Telegram.Bot.Types;
using System.Net;
namespace SendTxt-Photo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string Token = "adasdsadsadsadasds";
string channel_id = "@BestLaptop";
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
using (var stream = File.Open(@"image/1.jpg", FileMode.Open))
{
WebRequest req = WebRequest.Create("https://api.telegram.org/bot" + Token + "/sendMessage?chat_id=" + channel_id + "&text=" + textbox1.text);
req.UseDefaultCredentials = true;
WebRequest req1 = WebRequest.Create("https://api.telegram.org/bot" + Token + "/sendPhoto?chat_id=" + channel_id + "&Photo=" + stream );
req.UseDefaultCredentials = true;
req1.UseDefaultCredentials = true;
var result = req.GetResponse();
req.Abort();
var result1 = req1.GetResponse();
req1.Abort();
}
}
}
}
At first you must know that you send
...&photo=Sistem.IO.FileStream
instead your file. It's root cause of bad request. Take a look in debug.At second as you can see in api documentation
sendPhoto
method provide three types ofphoto
parameter:So using
multipart/form-data
required in your case. It can be simplified by using RestSharp for example like (code was generated by Postman, haven't tested):At third you can't sent photo above text in one message. For sending text and photo in one message use
caption
parameter ofsendPhoto
method. It provide 200 symbols length.PS Why not using the Telegram.Bot nuget package?