Telegram bot InlineResultChosen没有开火

时间:2017-08-14 12:12:12

标签: c# inline telegram telegram-bot

我使用C#和Telegram.Bot库制作了一个电报机器人。我已经做到了"内联"在BotFather中并将inlinefeedback设置为"启用"。

问题是当用户从内联答案中选择一个选项时,InlineResultChosen事件未触发,该消息正在发送给聊天但除了发件人本身之外没有人看到它。消息框中还有时钟(或移动版中的红色感叹号)。我正在使用内联语音消息,发件人可以听取它们。

这是我的代码:

internal class Program
{
    private static readonly List<InlineQueryResult> _queryResults 
        = new List<InlineQueryResult>();

    private static void Main(string[] args)
    {
        var key = "...";

        var sounds = new Dictionary<string, string>();
        sounds.Add("Sound one", "https://omg.lol/1.wav");
        sounds.Add("Sound two", "https://omg.lol/2.wav");

        foreach (var sound in sounds)
        {
            var voice = new InlineQueryResultVoice();
            voice.Url = sound.Value;
            voice.Title = sound.Key;
            voice.Id = sound.Key;
            _queryResults.Add(voice);
        }

        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += ProcessBot;

        if (bw.IsBusy == false)
        {
            bw.RunWorkerAsync(key);
        }

        Console.ReadLine();
    }

    private static void ProcessBot(object sender, DoWorkEventArgs e)
    {
        try
        {
            var key = e.Argument as string;
            var bot = new TelegramBotClient(key);

            bot.OnInlineQuery += AnswerInlineQuery;
            bot.OnInlineResultChosen += InlineResultChosen;
            bot.OnReceiveError += ErrorRecieved;
            bot.OnReceiveGeneralError += GeneralErrorReceived;

            bot.StartReceiving();

            Console.ReadLine();

            bot.StopReceiving();
        }
        catch (Telegram.Bot.Exceptions.ApiRequestException ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadLine();
        }
    }

    private static void GeneralErrorReceived(object sender, ReceiveGeneralErrorEventArgs e)
    {
        Console.WriteLine("Error catched: " + e.Exception.Message);
    }

    private static void ErrorRecieved(object sender, ReceiveErrorEventArgs e)
    {
        Console.WriteLine("Error catched: " + e.ApiRequestException.Message);
    }

    private static void InlineResultChosen(object sender, ChosenInlineResultEventArgs e)
    {
        var result = e.ChosenInlineResult;

        Console.WriteLine($"Sent a voice message by {result.From.Username}");
    }

    private static async void AnswerInlineQuery(
        object sender,
        InlineQueryEventArgs queryEventArgs)
    {
        var bot = sender as TelegramBotClient;
        var query = queryEventArgs.InlineQuery;

        await bot.AnswerInlineQueryAsync(
            query.Id,
            _queryResults.ToArray(),
            0);
    }
}

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。这是我的失败:语音信息格式错误。首先,我将其更改为.OGG,如Telegram开发人员文档中所述。但在这种情况下,声音在MacOS和iOS上无效!我已将所有声音更改为.MP3格式,现在它们在PC和Mac上都有效。现在只有一个问题:它似乎无法在Windows Phone 8上运行。