Skip to content

Commit

Permalink
added chats & business page.
Browse files Browse the repository at this point in the history
  • Loading branch information
wiz0u committed Aug 1, 2024
1 parent 1333b04 commit c64136c
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
- [Document & Animation](send-msg/document-animation-msg.md)
- [Native Polls](send-msg/native-polls-msg.md)
- [Other Messages](send-msg/other-msg.md)
- [Dealing with chats](chats.md)
- [Reply Markup](reply-markup.md)
- [Forward, Copy or Delete](forward-copy-delete.md)
95 changes: 95 additions & 0 deletions src/2/chats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Dealing with chats

All messages in Telegram are sent/received on a specific chat.
The `chat.Type` can be one of 4 types:

- `ChatType.Private`:
A private discussion with a user. The `chat.Id` is the same as the `user.Id` (positive number)
- `ChatType.Group`:
A private chat group with less than 200 users
- `ChatType.Supergroup`:
An advanced chat group, capable of being public, supporting more than 200 users, with specific user/admin rights, ...
- `ChatType.Channel`:
A broadcast type of publishing feed (only admins can write to it)

Additional notes:
- For groups/channels, the `chat.Id` is a negative number, and the `chat.Title` will be filled.
- For <u>public</u> groups/channels, the `chat.Username` will be filled.
- For private chat with a user, the `chat.FirstName` will be filled, and optionally, the `chat.LastName` and `chat.Username` if the user has one.

## Calling chat methods

All methods for dealing with chats _(like sending messages, etc..)_ take a `ChatId` parameter.

For this parameter, you can pass directly a `long` _(the chat or user ID)_,
or when sending to a public group/channel, you can pass a `"@username"` string

## Receiving chat messages

See chapter [Getting Updates](../3/updates/README.md) for how to receive updates & messages.

For groups or private chats, you would receive an update of type `UpdateType.Message` (which means only the field `update.Message` will be set)

For channel messages, you would receive an update with field `update.ChannelPost`.

For [business](../4/business) messages, you would receive an update with field `update.BusinessMessage`.

If someone modifies an existing message, you would receive an update with one of the fields `update.Edited*`

Note: if you use the `bot.OnMessage` event, this is simplified and you can just check the UpdateType argument.

> [!IMPORTANT]
> By default, for privacy reasons, bots in groups receive only messages that are targeted at them (reply to their messages, inline messages, or targeted `/commands@botname` with the bot username suffix)
> If you want your bot to receive ALL messages in the group, you can either make it admin, or <u>disable</u> the **Bot Settings** : [**Group Privacy** mode](https://core.telegram.org/bots/features#privacy-mode) in @BotFather
## Migration to Supergroup

When you create a private chat group in Telegram, it is usually a `ChatType.Group`.

If you change settings (like admin rights or making it public), or if members reach 200,
the Group may be migrated into a Supergroup.

In such case, the Supergroup is like a separate chat with a different ID.
The old Group will have a service message `MigrateToChatId` with the new supergroup ID.
The new Supergroup will have a service message `MigrateFromChatId` with the old group ID.

## Managing new members in a group

Bots can't directly add members into a group/channel.
To invite users to join a group/channel, you can send to the users the public link `https://t.me/chatusername` (if chat has a username), or invite links:

### Invite links

Invite links are typically of the form `https://t.me/+1234567890aAbBcCdDeEfF` and allow users clicking on them to join the chat.

You can send those links as a text message or as an `InlineKeyboardButton.WithUrl(...)`.

If your bot is administrator on a private (or public) group/channel, it can:
- read the (fixed) primary link of the chat:
```csharp
var chatFullInfo = await bot.GetChatAsync(chatId); // you should call this only once
Console.WriteLine(chatFullInfo.InviteLink);
```
- create new invite links on demand
```csharp
var link = await bot.CreateChatInviteLinkAsync(chatId, "name/reason", ...);
Console.WriteLine(link.InviteLink);
```

See also [some other methods for managing invite links](https://core.telegram.org/bots/api#exportchatinvitelink).

### Detecting new group members and changed member status

Note: Bots can't detect new <u>channel</u> members

The simpler approach to detecting new members joining a group is to handle service messages of type `MessageType.NewChatMembers`: the field `message.NewChatMembers` will contain an array of the new User details.
Same for a user leaving the chat, with the `message.LeftChatMember` service message.

However, under various circumstances (bigger groups, hidden member lists, etc..), these service messages may not be sent out.

The more complex (and more reliable) approach is instead to handle updates of type `UpdateType.ChatMember`:

* First you need to enable this specific update type among the `allowedUpdates` parameter when calling `GetUpdatesAsync`, `SetWebhookAsync` or `StartReceiving`+`ReceiverOptions`.
* Typically, you would pass `Update.AllTypes` as the allowedUpdates parameter.
* After that, you will receive an `update.ChatMember` structure for each user changing status with their old & their new status
* The `OldChatMember`/`NewChatMember` status fields can be one of the derived `ChatMember*` class: `Owner`/`Creator`, `Administrator`, `Member`, `Restricted`, `Left`, `Banned`/`Kicked`)
1 change: 1 addition & 0 deletions src/4/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- [Proxy](proxy.md)
- [Mini Apps](webapps.md)
- [Business Features](business.md)
- [Passport](passport/)
- [Quickstart](passport/quickstart.md)
- [Files & Documents](passport/files-docs.md)
Expand Down
6 changes: 6 additions & 0 deletions src/4/business.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Business Features

> This chapter is not yet written.
See https://telegram.org/blog/telegram-business
and https://core.telegram.org/bots/features#bots-for-business
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [Document & Animation](2/send-msg/document-animation-msg.md)
- [Native Polls](2/send-msg/native-polls-msg.md)
- [Other Messages](2/send-msg/other-msg.md)
- [Dealing with chats](2/chats.md)
- [Reply Markup](2/reply-markup.md)
- [Forward, Copy or Delete](2/forward-copy-delete.md)
- [Intermediate](3/README.md)
Expand All @@ -28,6 +29,7 @@
- [Advanced](4/README.md)
- [Proxy](4/proxy.md)
- [Mini Apps](4/webapps.md)
- [Business Features](4/business.md)
- [Passport](4/passport/README.md)
- [Quickstart](4/passport/quickstart.md)
- [Files & Documents](4/passport/files-docs.md)
Expand Down

0 comments on commit c64136c

Please sign in to comment.