How to Solve Disturbance in My Bot in C#

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



Leave a reply



Submit