Telegram Bot: get chat Id group - c#

I've created a Telegram Bot using C# and I want to see who is using my bot when sending message.
I can get when people send a message to my bot in private chat but in the Groups, I can't get Username who is using Bot and it's return the GroupName.
The method I use is
var me = Bot.GetChatAsync(e.Message.Chat.Id).Result;

From doc this field User is optional for groups.

You can see who send message in e.Message.From, this property returns who send it as User.
And you should await methods that is awaitable.
And in your case Bot.GetChatAsync, this will return Chat from ChatId.
It means: Info about chat from Id.
And you pass Chat not ChatId to Bot.GetChatAsync

Related

Telegram Bot API C# How to get All Pinned Messages?

Telegram added the feature to pin more than one message. I'm doing it like this:
var chat = botClient.GetChatAsync(chatid).Result;
pinnedmsgtxt = chat.PinnedMessage.Text;
pinnedmsg= chat.PinnedMessage;
but I can only get the last message that was pinned. Any way to get all of them? Am I missing something? I'm using https://github.com/TelegramBots/telegram.bot
Use messages.search method with inputMessagesFilterPinned filter.
Please read more here:
Pin API
Message Search API
Pinned messages filter

Access previous message content from a user reply message on Telegram

Does anyone know how can I access a previous message content when a user sends a reply message on Telegram?
for example:
James: hello
me: reply on James message with "hello friend"
here i want to get the hello message content or its message id in my telegram bot to process a command.
sth like this:
if(message.Contains("/delete")){
bot.DeleteMessageAsync(chatId, PreviousMessage or OriginalMessage ID)
}
PreviousMessage or OriginalMessage ID are equals to MessageId
but instead of MessageId i want to use sth like those two
I have checked telegram bot documentation and I did not find anything
How can i do it?
(Im using getUpdates method)
sorry for my poor English
You can find the original message ID in message.reply_to_message.message_id. Naming could differ depends on the library you use. More information and other attributes that could be useful you can find in the official Telegram's documentation for the Message object.

How do I send a notification to a user in Teams via the Bot Framework?

I have a Bot created with v4 of the Microsoft Bot Framework. I can successfully use this bot in the "Test in Web Chat" portion in the Azure Portal. I can also successfully use this bot in an app that I've created in Microsoft Teams. I now want to send a notification from the "Test in Web Chat" piece to a specific user in Teams. For example, in the "Test in Web Chat" piece, I'd like to enter
Hello someuser#mytenant.com
When this is sent via the "Test in Web Chat" piece, I'd like to show "Hello" in Microsoft Teams to only someuser#mytenant.com. I have successfully tokenized the string from the "Test in Web Chat". Thus, I know what I want to send, and who I want to send it to. However, I do not know how to actually send it.
Currently, I have the following in my bot:
public class EchoBot : ActivityHandler
{
private ConcurrentDictionary<string, ConversationReference> _conversationReferences;
public EchoBot(ConcurrentDictionary<string, ConversationReference> conversationReferences)
{
_conversationReferences = conversationReferencs;
}
private void AddConversationReference(Activity activity)
{
var reference = activity.GetConversationReference();
_conversationReferences.AddOrUpdate(reference.User.Id, reference, (key, newValue) => reference);
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> context, CancellationToken cancellationToken)
{
AddConversationReference(context.Activity as Activity);
var parameters = GetParameters(); // Parses context.Activity.Text;
// Send a message to the target (i.e. someuser#mytenant.com)
var connection = new Microsoft.Bot.Connector.ConnectorClient(new Uri(context.Activity.ServiceUrl));
var tenant = context.Activity.GetChannelData<TeamsChannelData>().Tenant;
// how do I send the message to parameters.Target?
// Confirm message was sent to the sender
var confirmation = $"Message was sent to {parameters.Target}.";
await context.SendActivityAsync(MessageFactory.Text(confirmation));
}
}
I've reviewed how to send proactive notifications to users. However, I've been unsuccessful in a) getting the user specified in parameters.Target and b) sending a notification to that user. What am I missing?
First, you'll need to map user#email.com to their Teams userId (maybe with a static dictionary), which is in the format of:
29:1I9Is_Sx0O-Iy2rQ7Xz1lcaPKlO9eqmBRTBuW6XzXXXXXXXXMij8BVMdBcL9L_RwWNJyAHFQb0TXXXXXX
You can get the Teams UserId by either:
Querying the roster, or
Having the user message the bot, and setting a breakpoint on an incoming message, looking at the Activity.ChannelData for the Teams userId, or
Dynamically build a static dictionary of all incoming messages that stores the user's email mapped to their Teams userId (I believe both are found in Activity.ChannelData).
Note: #1 and #2 both require a user to message the bot, first, which sort of defeats the purpose of proactive messages
After you have the appropriate Teams IDs, you just send a proactive message to a Teams user. The end of this link also mentions trustServiceUrl, which you may find handy if you run into permissions/auth issues when trying to send a proactive message.

Send proactive message for Skype using Direct Line API

I am using Direct Line API v4 to send a message to my Web Chat proactively using an existing conversation (by passing the existing conversation ID, saved when the conversation started).
Code:
var client = new DirectLineClient("secreat");
var activity = new Activity();
activity.From = new ChannelAccount("userid");
activity.Type = "resume";
activity.ChannelId = "directline";
activity.Text = "Hi";
activity.Conversation = new ConversationAccount(id: "existingconverstaionid");
var result = client.Conversations.PostActivity("existingconverstaionid", activity);
This code runs fine and I am able to continue with my existing conversation with my Web Chat channel. I would like this same functionality to work for my Skype channel, so I have replaced userid and existingconversationid I received from the Skype conversation, but this does not work...
You can't use the DirectLineClient to broadcast a message to the Skype channel. It is actually quite easy, since you can just use the BotFramework SDK to send a proactive message, as long as you saved the conversation info.
Read more about sending proactive messages using BotFramework v4 or have a look at the example.

How to send message in the group using group name not a chat id on telegram bot using c#?

Hello every one i have right now go for the my app to sync with telegram bot.and i need to send some messages on group on the telegram bot.for ex. i have create one one group on the telegram bot like group name is "Rock" and my bot name is like "ABC". now i need to from my c# side need some create api and send message using group name. i have find many links but all link give idea with chat id. but i want work with group name.here i have create one api for the send message but it's working with chat id and not work for the group.
Here this is my api =>
[System.Web.Http.AcceptVerbs("GET", "POST")]
public void sendMessage(string destID, string text)
{
try
{
Bot.SendTextMessageAsync(destID, text);
}
catch (Exception e)
{
Console.WriteLine("err");
}
}
any one know how can do that please let me know. using group name i want to send message on telegram bot.
You can't use title as parameter to send message, think about two group have same name? :)
If you don't want to send via Chat ID, try group username.
Unique identifier for the target chat or username of the target channel (in the format #channelusername)
The group name isn't unique and telegram can't use that for sending a message. Because a name may be used for several groups. Which one should be selected?
You have no another option but if you want to simplify your code, you can store group-id as const variable:
public const double Rock= '164865465465';
and use like this:
SendMessage(Rock, text);

Categories