I've been trying to solve this for three days and none pf the solutions I've found online seem to work, so I am going to have to ask for help.
I want to create a Telegram "Group Chat" to be used by members of a club I'm in. I created the chat named with the initials of the club, like: "ABCD" and added some members. Now I want to automate the sending of occasional messages to the group for all members to see. Weather forecast, random photograph from our gallery, etc.
Using #BotFather I created a bot called "ABCDbot" and noted the token for that bot. Now I have two "ABCD"s on my browser left side-panel. Selecting one gives me "ABCD bot" and selecting the other gives me "ABCD 123 members".
Using a perl script and LWP I can send a photo using
#!/usr/bin/perl -w
use feature 'say';
use LWP;
my $api = LWP::UserAgent->new ();
my $chat_id = '1234567890';
my $photo = '/home/user/gallery/photo999.jpg';
my $response = $api->post(
"https://api.telegram.org/bot<ABCDbot's token>/sendPhoto",
[ 'chat_id' => $chat_id,
'caption' => 'Photo Randomly Selected by the gallery',
'photo' => $photo,
],
'Content_Type' => 'form-data',
);
if ($response->is_success) {
say "Response..... Success!";
} else {
say "Response..... Failure!";
}
This works, providing I give it a legitimate chat_id and a legitimate file to send.
But the trouble is: I can't find the chat_id for the group chat with 123 members! Every method I've tried now proves to be obsolete or simply doesn't return the desired chat_id for the ABCD group chat. I can get my own chat ID or that of individual members of the group, or of the bot itself, and can successfully send photos, messages, etc to those destinations, but I just can't send anything to the group.
Can anybody walk me through the process of getting the chat_id for my group chat? Or direct me to a document describing an up-to-date, working method for obtaining same?
Assistance much appreciated.
Method 1 (WebZ)
This is based on JayeshRocks's question with some extra steps to make the ID work with Bot API. Thanks to him first.
Login to Telegram WebZ.
Open the chat you want to get its ID.
Your browser's address should look like https://web.telegram.org/z/#-1527776602.
Remove the protocol, domain and path keeping the anchor so your result looks like #-1527776602.
Replace "#-" with "-100" so it looks like -1001527776602.
You can now use your final result which should look like -1001527776602.
Method 2 (Private Supergroups)
If the chat is a private channel/supergroup, you can do the following:
Copy a link of a message. (It will look like https://t.me/c/1527776602/1002.)
Remove the protocol and domain name, so it looks like c/1527776602/1002.
Remove the first and last path, so it looks like 1527776602.
Append "-100" to the beginning of the result, so it looks like -1001527776602.
You can now use your final result which looks like -1001527776602.
Method 3 (Third-Party Bots)
If you trust 3rd party bots, there are many of them. A known one is #MissRose_bot which you can add it to your group and use its /id command. Another one is #usinfobot which works inline and only for public chats.
To get a telegram group ID you need to open the group on https://web.telegram.org/z/ when you do so click on the group you want to gain the ID of then if you look at the URL it will say something like https://web.telegram.org/z/#-1234567 and the numbers there is the ID of the telegram group!
Hope this helps
Related
I have created telegram bot using telegraf.js
The bot is working correctly, however I need to handle a different thing if the user send message from inside bot directly, let's say the bot should replay with help commands documentations (for example).
the question is:
how to recoginze where the user start chatting? from inside chat group or from inside bot directly?
I tried
var groupInfo =await ctx.telegram.getChat()
without success
I thing the solution would be simple, but I can't find it till now.
thank you in advanced.
You should checkout Telegram docs for Chat type. It has a field called Type and according to the docs:
Type of chat, can be either “private”, “group”, “supergroup” or “channel”
So in telegraf.js you can check the field this ways:
bot.on('text', (ctx) => {
return ctx.reply(`Chat type is: ${ctx.message.chat.type}`)
})
In your case, ctx.message.chat.type == "private" would be messages that are sent to your bot privately and ctx.message.chat.type == "group" or ctx.message.chat.type == "supergroup" are messages sent to groups.
At first i should emphasis that this is a question about telegram GROUP NOT CHANNEL.I need to get group id to send message via telgram api.
I have review this link .
using #rawDataBot needs to add bot to group, that is not possible most of the times. CuteGram does not login (does not send login code) .so i can say none of the proposed method works.
so is there a new method -except adding a bot to the group- to get telgram group ID ?
if the answer is no, i need to know if it is possible to send message to group by using group name?!
the easiest way is to add #chatBotRaw the bot will dump raw data, and you can copy their chat_id or anything else you need,
Remember to remove the bot afterwards because it dumps raw data of every message sent/received in the group.
the simplest way i found is
open web-telegram in a browser
right click on the group name on the left menu
click 'inspect' button
you will see the group id in the attribute data-peer-id="-xxxxxxxxxx" or peer="-xxxxxxxxxx"
You can get chat id throw object "chat"
You can set middleware which handle new update
Example on Node.JS: bot.on('text', ctx => console.log('Chat id is:', ctx.chat.id))
P.S. group id and chat id are same
if using a bot is acceptable, you can use #username_to_id_bot - no need to add it to a group, just send username or invite link and get the id
I want to add an unsubscribe link in emails being sent to the user. I am unsure on how to achieve it. I want the user to be able to unsubscribe without having to log in. Any suggestions on how to achieve this would be appreciated. Thanks
I'll assume you know how to send an email with django, and other basic things.
For every email you send to them, you have to build the unsubscribe link first, like the following. Note that the following has a url pattern, and a model that takes a uuid field (uuid4 to be specific, because it's the safest one). The reason why you want to do it this way is so random people can't go to YourWebsite.com/unsub_email/1, /2, /3, /4, etc, and unsub as many people as they possibly can by guessing the number. By doing it the way I show, there's something like a chance of 1 in 100, with like 50+ zero's after it that they would even get 1. So it's safer.
def send_user_an_email(id_of_your_object):
users_email = UsersEmail.objects.get(id=id_of_your_object)
unsub_link = 'https://www.YourWebsite.com/unsub_email/{}/{}/'.format(users_email.id, users_email.random_uuid)
subject = 'Your subject line.'
message = 'Your message body.\n\nGo here to unsubscribe: {}'.format(unsub_link)
mail_sent = send_mail(subject, message, 'DoNotReply#YourWebsite.com', [users_email.users_email_char_field])
return mail_sent
Then you just write a function to let them click a button on that page, and they get unsubbed, or however else you want to do it. An even safer option would be to show them a valid unsub page, but make them type out their email again, but don't show them the email related to the valid url pattern. That extra step would make it that much harder for someone who'd want to write a bad script to unsub everyone on your list.
Let's say, I've joined TELEGRAM group...
I am just a typical member of GROUP (and thus, cant use any bots there.. ?) so, I am unable to find out the way, how to get GROUP ID.
New Update
Just Simply Forward a message from your channel to This Bot: (https://telegram.me/getidsbot)
Update
1: Goto (https://web.telegram.org)
2: Goto your Gorup and Find your link of Gorup(https://web.telegram.org/#/im?p=g154513121)
3: Copy That number after g and put a (-) Before That -154513121
4: Send Your Message to Gorup
bot.sendMessage(-154513121, "Hi")
I Tested Now and Work like a Charm
Node.js:
Try using TelegramBot#getChat():
bot.getChat("#channelusername").then(function(chat) {
// 'chat' is a Chat object
console.log(chat.id);
});
See API getChat() method and Chat object.
Hope that helps.
There is a unofficicl Plus Messenger client for Android users, and you can see ID in group/channel info.
Supergroup and Channel will looks like 1068773197, which is -1001068773197 for bots (with -100 prefix).
If you just want to obtain channel/user ID, forward message to #RawDataBot.
there are lots of ways to do so.
simplest one: download plus messenger which is a fork of telegram. it shows every channel's id in the about page of that channel.
https://play.google.com/store/apps/details?id=org.telegram.plus&hl=en
thanks to #Sean:
Supergroup and Channel will looks like 1068773197, which is -1001068773197 for bots (with -100 prefix).
Above method works for channels, for groups you may use this method:
Just forward a single message from that chat to #RawDataBot. it will reply you with a json data containing chatid.
via code:
If you are a member of a group, you should receive updates from that group when ever there is any activity from that group. The updates will contain a chats list-element from which you can get a Channel which has the following relevant fields:
id: group_id,
title: "the_group_title",
username: "group_username"
As of my experience ,there are two popular libraries,
python-Telethon --->Telegram Client Library(uses api_id,api_hash)
python-Telegram-bot ---->Telegram Bot (uses api token)
There are lot of ways to get the user_id ,group_id,channel_id .
To get the these ids use Telethon client library
from telethon import TelegramClient,sync
api_id="xxx" #get from telegram website
api_hash="yyy" #get from telegram website
client=TelegramClient(session_object,api_id,api_hash)
client.start()
#To get the channel_id,group_id,user_id
for chat in client.get_dialogs():
print('name:{0} ids:{1} is_user:{2} is_channel{3} is_group:{4}'.format(chat.name,chat.id,chat.is_user,chat.is_channel,chat.is_group))
That is all about, it will print name and id of channel,group,user.
Also, it will check wheather the id is belong to channel or group or user
generally, Channel id starts with negrative(eg:-1001109500936) starts with (-100)
group id is normal and starts with negative
user id starts with positve
Another way is to use 'plus messanger app'
To see all the group,channel,user id
Cheers
Another Simple Way,
client=TelegramClient(session,api_id,api_hash)
client.start()
destination_entity_name="Type User(may be bot) or group or channel name"
entity=client.get_entity(destination_entity_name)
print(entity.stringify()) #All paratmeters
print(entity.id) #user(bot also considered as user) or group
That should be obtainable using tg-messenger-cli: https://github.com/vysheng/tg I haven't had time to try it out yet but friend has made some automated messages for his daughter. Should be quite versatile.
After hours spent, I was able to find the ID of GROUPS using CuteGram app.
Open group there, and click "COPY" icon, that opens a folder, and in the address you will see the ID.
Invite your bot to your group
and use ur bot to text /myid
then use the GetUpdates api, you shall have your group ID
I'm trying to use a Telegram BOT to send messages to a group. First, I thought that it'd be enough to know the group chat id to accomplish that, but it's not. The BOT MUST be part of that group. OK, it kind of make sense, but the problem is: When you add a BOT into a group (a large group in this case) everyone start seeing a new icon on their devices, a "slash" icon. And what do they do ? They click on it, see the list of commands, choose one of them, and all of a sudden everyone is getting a new message from the group: a "/something". Imagine dozens of people doing that ? It's pretty annoying. So, any of these would work for me:
1) Can I send messages from a BOT to a group without having that BOT in the group ?
2) Can I have a kind of "no methods" BOT, that only send messages ?
3) Can I disable "slash" icon from clients so I won't have a "bot method war" in the group ?
Thank you
No, you cannot have bots send messages to a group without being a part of that group.
You can simply not set commands with BotFather, and then clients will have no commands to display.
It is always there if a bot is in the current chat, but here is what it does with no commands set in BotFather:
I got a much better solution: there is the possibility to customize the commands directly by code, also depending from the context (i.e. private chat, groups, etc...)
This example was done by using Telegraf but that's not so different from basic code
bot.start(function(ctx) {
// If bot is used outside a group
ctx.telegram.setMyCommands(
[
{
"command": "mycommand",
"description": "Do something in private messages"
}, {
"command": "help",
"description": "Help me! :)"
}
],
{scope: {type: 'default'}}
)
// If bot is used inside a group
ctx.telegram.setMyCommands(
[
// <-- empty commands list
],
{scope: {type: 'all_group_chats'}}
)
ctx.reply('Hello! I\'m your super-cool bot!!!')
})
Bonus Point, you can also manage the command behavior by checking the source.
So, for example, if a user in a group still try to use manually your command and you don't want to execute anything:
bot.help(function(ctx) {
// Check if /help command is not triggered by a private chat (like a group or a supergroup) and do nothing in that case
if (ctx.update.message.chat.type !== 'private') {
return false
}
ctx.reply('Hi! This is a help message and glad you are not writing from a group!')
})