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.FileStreaminstead your file. It's root cause of bad request. Take a look in debug.At second as you can see in api documentation
sendPhotomethod provide three types ofphotoparameter:So using
multipart/form-datarequired 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
captionparameter ofsendPhotomethod. It provide 200 symbols length.PS Why not using the Telegram.Bot nuget package?