简体   繁体   English

电报机器人InlineResultChosen不触发

[英]Telegram bot InlineResultChosen not firing

I've made a telegram bot using C# and Telegram.Bot library. 我已经使用C#和Telegram.Bot库制作了一个电报机器人。 I've made it "inline" in BotFather and set inlinefeedback to "enabled". 我在BotFather中将其设置为“内联”,并将inlinefeedback设置为“已启用”。

The problem is when a user choose an option from inline answer, InlineResultChosen event not firing, the message is sending to chat but nobody see it except sender itself. 问题是当用户从内联答案中选择一个选项时,InlineResultChosen事件未触发,消息正在发送聊天,但发件人本身没有人看到它。 Also there are clocks in message box (or red exclamation point in mobile version). 消息框中也有时钟(或移动版本中的红色感叹号)。 I am using inline voice messages, and they are can be listened by sender. 我正在使用嵌入式语音邮件,发件人可以收听它们。

Here is my code: 这是我的代码:

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);
    }
}

I found the solution. 我找到了解决方案。 It was my fail: voice messages was in wrong format. 这是我的失败:语音消息格式错误。 First I changed it to .OGG like described in Telegram developers documentation. 首先,如Telegram开发人员文档中所述,将其更改为.OGG。 But in this case sound does not work on MacOS and iOS! 但是在这种情况下,声音在MacOS和iOS上不起作用! I've changed all my sounds to .MP3 format and now they are working both on PC and Macs. 我已将所有声音更改为.MP3格式,现在它们在PC和Mac上均可使用。 Only one problem now: it seems it doesn't work on Windows Phone 8. 现在只有一个问题:似乎在Windows Phone 8上不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM