2016-01-20 11 views
6

Provo a creare messaggi con la tastiera personalizzata. Così mando richiesta conTastiera personalizzata di Telegram Bot in С #

reply_markup = {"keyboard":[["1"],["2"]],"resize_keyboard":"True","one_time_keyboard":"True"} 

Ma, non funziona.

ho provato tutti i tipi di contenuto:

  1. application/x-www-form-urlencoded (crea messaggio con valore predefinito della tastiera )
  2. application/json (creare un messaggio con la tastiera di default)
  3. multipart/form-data (non funziona affatto, nonostante this Post)

ho anche provato a inviare il messaggio da 2 modi diversi. Watt sbagliato con questo codice?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Net; 

namespace DutyReminder 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 

     string message = "message"; 
     string message1 = "message1"; 
     string botid = "165749848:AAGtjn42bajF-WxdKosTF07sLwJPYlqiDZE"; 
     string chatid = "38651047"; 

     Sender.send("", "https://api.telegram.org/bot" + botid + "/sendmessage?chat_id=" + chatid + "&text=" + message + "&reply_markup={\"keyboard\":[[\"1\"],[\"2\"]],\"resize_keyboard\":\"True\",\"one_time_keyboard\":\"True\"}"); 
     Sender.HttpPost("https://api.telegram.org/bot" + botid + "/sendmessage?chat_id=" + chatid + "&text=" + message1 + "&reply_markup={\"keyboard\":[[\"1\"],[\"2\"]],\"resize_keyboard\":\"True\",\"one_time_keyboard\":\"True\"}", ""); 

    } 
} 

static class Sender 
{ 
    static public void send(string message, string url) 
    { 
     // Create a request using a URL that can receive a post. 
     WebRequest request = WebRequest.Create(url); 
     // Set the Method property of the request to POST. 
     request.Method = "POST"; 
     // Create POST data and convert it to a byte array. 
     //string postData = "{\"value1\":\"" + message + "\"}"; 
     string postData = message; 
     byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
     // Set the ContentType property of the WebRequest. 
     request.ContentType = "application/x-www-form-urlencoded"; 
     // Set the ContentLength property of the WebRequest. 
     // request.ContentLength = byteArray.Length; 
     // Get the request stream. 
     Stream dataStream = request.GetRequestStream(); 
     // Write the data to the request stream. 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     // Close the Stream object. 
     dataStream.Close(); 
     // Get the response. 
     WebResponse response = request.GetResponse(); 
     // Display the status. 
     Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
     // Get the stream containing content returned by the server. 
     dataStream = response.GetResponseStream(); 
     // Open the stream using a StreamReader for easy access. 
     StreamReader reader = new StreamReader(dataStream); 
     // Read the content. 
     string responseFromServer = reader.ReadToEnd(); 
     // Display the content. 
     Console.WriteLine(responseFromServer); 
     // Clean up the streams. 
     reader.Close(); 
     dataStream.Close(); 
     response.Close(); 
    } 


    static public string HttpPost(string URI, string Parameters) 
    { 
     System.Net.WebRequest req = System.Net.WebRequest.Create(URI); 
     // req.Proxy = new System.Net.WebProxy(ProxyString, true); 
     //Add these, as we're doing a POST 
     req.ContentType = "application/x-www-form-urlencoded"; 
     req.Method = "POST"; 
     //We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value& 
     byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters); 
     req.ContentLength = bytes.Length; 
     System.IO.Stream os = req.GetRequestStream(); 
     os.Write(bytes, 0, bytes.Length); //Push it out there 
     os.Close(); 
     System.Net.WebResponse resp = req.GetResponse(); 
     if (resp == null) return null; 
     System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()); 
     return sr.ReadToEnd().Trim(); 
    } 

} 
} 
+0

Quale errore si fa a ottenere ? Potrebbe essere necessario aggiungere try-catch-Blocks al codice per ottenere un messaggio di errore dettagliato. –

+0

Non ci sono errori, ma non c'è nemmeno una tastiera personalizzata http://f6.s.qip.ru/echeMYh9.png Sembra che il telegramma ignori il parametro reply_markup. – Grisha

+0

Quindi, quale risposta ottieni? Penso che la prima domanda sia, c'è un problema di comunicazione o un problema di comando. –

risposta

5

Utilizzare la seguente invece di inviare un messaggio URI:

var bot = new Api("YourApiToken"); 
. 
. 
. 

var rmu = new ReplyKeyboardMarkup(); 

rmu.Keyboard = 
    new string[][] 
    { 
     new string[] {"1-1", "1-2"}, 
     new string[] {"2"}, 
     new string[] {"3-1", "3-2" , "3-3" } 
    }; 

await bot.SendTextMessage(update.Message.Chat.Id, msg, false, 0, rmu); 


AGGIORNATO
Con la nuova versione di API:

var bot = new Api("YourApiToken"); 
. 
. 
. 

var rkm = new ReplyKeyboardMarkup(); 

rkm.Keyboard = 
    new KeyboardButton[][] 
    { 
     new KeyboardButton[] 
     { 
      new KeyboardButton("1-1"), 
      new KeyboardButton("1-2") 
     }, 

     new KeyboardButton[] 
     { 
      new KeyboardButton("2") 
     }, 

     new KeyboardButton[] 
     { 
      new KeyboardButton("3-1"), 
      new KeyboardButton("3-2"), 
      new KeyboardButton("3-3") 
     } 
    }; 

await bot.SendTextMessage(update.Message.Chat.Id, msg, false, false, 0, rkm); 
+0

Grazie, per la risposta! Che struttura usi? – Grisha

+0

@Grisha, Solo in' Nuget' Tipo ' Install-Package Telegram.Bot' –

+0

@SiyavashHamdi: http://stackoverflow.com/questions/39884961/create-dynamic-keyboard-telegram-bot-in-c-sharp-mrroundrobin-api –