Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i want to use botsharp library in win form application in C# for use telegram bot
but i To face whith this error
( 'Message' is an ambiguous refrence between 'System.Windows.Forms.Message' and 'TelegramBotSharp.Types.Message')




foreach (Message m in result)
and
private static void ControlMessages1(Message m)

What I have tried:

C#
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelegramBotSharp;
using TelegramBotSharp.Types;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static TelegramBot bot;
        public Form1()
        {
            InitializeComponent();


        }

        private void button1_Click(object sender, EventArgs e)
        {
            bot = new TelegramBot("146900***********************-FyoSPg");
             new Task(PollMessages1).Start();
        }

        static async void PollMessages1()
        {
            while (true)
            {
                var result = await bot.GetMessages();
                foreach (Message m in result)
                {
                    if (m.Chat != null)
                    {
                        Console.WriteLine("[{0}] {1} : {2}", m.Chat.Title, m.From.Username, m.Text);
                    }
                    else
                    {
                        Console.WriteLine("{0}: {1}", m.From.Username, m.Text);
                    }
                    ControlMessages1(m);
                }

            }
        }

        private static void ControlMessages1(Message m)
        {
            if (m.Text == null) return;
            MessageTarget target = (MessageTarget)m.Chat ?? m.From;

                bot.SendMessage(target, "hello");
         }
    }
}
Posted
Updated 24-Apr-16 16:06pm
v2

1 solution

You fail to understand such basic and simple thing as namespaces, so I cannot advise to fix this elementary bug and go ahead with Telegram Bots or anything advanced at all. You should better learn the very basics of programming and .NET, which is much more productive to learn on simplest console-only applications.

However, the fix is: 1) use full type names, System.Windows.Forms.Message or TelegramBotSharp.Types.Message, 2) alternatively, modify using to rename one of both of the type in question; for example using TelegramBotSharpMessage = TelegramBotSharp.Types.Message, 4) think if you need System.Windows.Forms in this time at all: one of the ugliest purely methodical mistakes of the beginners is having too much "using", in particular, not cleaning up some auto-generated code, 5) if it is really needed, you can use even better alternative: segregate UI stuff from Bot stuff, so the Forms and TelegramBotSharp namespace never clash (but it won't free you from learning the name resolution techniques which you need to use in some other cases anyway).

Learn. Don't leave it as is. This topic is one of the simplest in .NET. If you ever leave issues without full understanding, you will soon get lost in the tons of accumulated problems.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900