How to solve disturbance in my bot in c#?
As @Andy Lamb wrote in a comment, your problem is that you are managing only one "game", so every player interacts with each other.
You must find a way to identify the sender of each message, and manage a "game" for each player.
A game object should be an instance of a class, maintaining all the data which is linked to a single player game (e.g. desired_word, etc). Your while (true)
loop should look something like this:
while (true) {
var updates = await bot.MakeRequestAsync(new GetUpdates() { Offset = offset });
foreach(var update in updates) {
var sender = GetSender(update);
var game = RetrieveGameOrInit(sender);
// ... rest of your processing, but your code is a little messy and
// you have to figure out how to refactor the processing by yourself
game.Update(update);
// do something with game, and possibly remove it if it's over.
}
}
public string GetSender(UpdateResponseOrSomething update)
{
// use the Telegram API to find a key to uniquely identify the sender of the message.
// the string returned should be the unique identifier and it
// could be an instance of another type, depending upon Telegram
// API implementation: e.g. an int, or a Guid.
}
private Dictionary<string, Game> _runningGamesCache = new Dictionary<string, Game>();
public Game RetrieveGameOrInit(string senderId)
{
if (!_runningGamesCache.ContainsKey(senderId))
{
_runningGamesCache[senderId] = InitGameForSender(senderId);
}
return _runningGamesCache[senderId];
}
/// Game.cs
public class Game
{
public string SenderId { get; set; }
public string DesiredWord { get; set; }
// ... etc
public void Update(UpdateResponseOrSomething update)
{
// manage the update of the game, as in your code.
}
}
Hope it helps!
create dynamic Keyboard telegram bot in c# , MrRoundRobin API
You could use a separate function to get an array of InlineKeyboardButton
private static InlineKeyboardButton[][] GetInlineKeyboard(string [] stringArray)
{
var keyboardInline = new InlineKeyboardButton[1][];
var keyboardButtons = new InlineKeyboardButton[stringArray.Length];
for (var i = 0; i < stringArray.Length; i++)
{
keyboardButtons[i] = new InlineKeyboardButton
{
Text = stringArray[i],
CallbackData = "Some Callback Data",
};
}
keyboardInline[0] = keyboardButtons;
return keyboardInline;
}
And then call the function:
var buttonItem = new[] { "one", "two", "three", "Four" };
var keyboardMarkup = new InlineKeyboardMarkup(GetInlineKeyboard(buttonItem));
Can I get a phone number by user id via Telegram Bot API?
It's possible with bots 2.0 check out bot api docs.
https://core.telegram.org/bots/2-0-intro#locations-and-numbers
https://core.telegram.org/bots/api#keyboardbutton
Related Topics
How to Store (And Manage) Application License Information
Send JSON via Post in C# and Receive the JSON Returned
If My Interface Must Return Task How to Have a No-Operation Implementation
Datetime.Parse("2012-09-30T23:00:00.0000000Z") Always Converts to Datetimekind.Local
Best Practices for Serializing Objects to a Custom String Format for Use in an Output File
Asynchronously Sending Emails in C#
Calling Async Methods from Non-Async Code
Could Not Find Installable Isam
Regular Expression to Allow Backslash in C#
How to Store JSON in an Entity Field with Ef Core
How Can a Wpf Usercontrol Inherit a Wpf Usercontrol
When Using a Settings.Settings File in .Net, Where Is the Config Actually Stored
Retrieving Files from Directory That Contains Large Amount of Files