TL-Schema - how to get access_hash of private channel? - ssl

To send messages (messages.sendMessage), you need channel_id and access_hash. For any public channels or chats, I can get it using contacts.resolveUsername and pass the username. But what about private channels, Is it accessible only by link? I Can't find a solution. Thanks!

You can get it either by joining the channel and checking the retval or iterating your dialogs (client.iter_dialogs)

Related

How to check if my account already in chat, channel, private channel, private group?

I want to check if my account is member of the channel. How can I do this if I only have valid invite link
With the current telethon API, you can't check if you're a member of a channel or private group using an invite link. However, you can resolve the invite link into channel ID and then use iter_dialogs to check if there is a same ID.
To avoid FloodWait, it's better to do iter_dialogs once and save a local copy for yourself. You may update your local copy when you join new channels and groups.
For public groups you can iter_participants and find yourself. However, the former method still works here as well.
You can do:
async for dialog in client.iter_dialogs():
if dialog.entity.megagroup:
group_name = dialog.entity.title
members_count = str(dialog.entity.participants_count)
print(f'GROUP: {group_name} - ({members_count} members)')
Here I checked if the chat is a megagroup, if so then it will print participants count and group's name.

How to forward a message in Telegram API

There are 2 methods in Telegram API that forward message:
messages.forwardMessage
messages.forwardMessages
I want to use forwardMessage method to forward a message from a channel, group or user to another one. Definition of this method is:
messages.forwardMessage#33963bf9 peer:InputPeer id:int random_id:long = Updates;
As you see this method has 3 input parameters:
peer that represents the channel, group or user that we forward message to. (Destination)
id that is message_id.
random_id that has internal use.
As we know the message_id is a unique number in a chat. so a message_id in a group has refers to a message that differs with the same message_id in other group.
So the main question is that how we determine the source peer of forwarding? Because the source peer is not determined by message_id.
P.S: My question is about methods in Telegram API, not Telegram Bot API.
There seems to an issue with ForwardMessageRequest which doesn't specify the source chat. Obviously message_id is not unique and through my tests I noticed wrong messages will be forwarded by just specifying the message_id. And I noticed message_id is not unique.
But the issue doesn't exist with ForwardMessagesRequest. Following is an example how to use the ForwardMessagesRequest version.
Forwarding Example:
Here is the code I used for testing (I am using Telethon for python, but it won't matter since it's directly calling telegram API):
source_chat = InputPeerChannel(source_chat_id, source_access_hash)
total_count, messages, senders = client.get_message_history(
source_chat, limit=10)
for msg in reversed(messages):
print ("msg:", msg.id, msg)
msg = messages[0]
print ("msg id:", msg.id)
dest_chat = InputPeerChat(dest_chat_id)
result = client.invoke(ForwardMessagesRequest(from_peer=source_chat, id=[msg.id], random_id=[generate_random_long()], to_peer=dest_chat))

Obtaining chat_id having chat link in Telegram API

I am using Telegram API to develop a program to join Telegram groups or channel by their links.
Methods that join group or channel (e.g. channels.joinChannel) need chat_id or channel_id, but I have only the links of the groups or channels (e.g. #channel_username or https://t.me/channel_username or https://t.me/joinChat/xxxxx)
How can I obtain chat_id or channel_id of a group or channel having its link?
P.S: I'm not the admin of these groups or channels.
I found the answer:
First we must use checkChatInvite method. It uses the chat link as input parameter and outputs the chat specifications includes chat_id.
Then we use joinChat method method. it uses the chat_id got from the previous step and joins to that group or channel.
Selected answer seems to be outdated. In recent versions there is checkChatInviteLink call, but it requires the chat url to start with https://t.me/joinchat/
If you want to resolve a link like https://t.me/chatname, you can use searchPublicChat API call.
This works for me (using https://github.com/alexander-akhmetov/python-telegram):
def resolve_channel_link(tg, link):
if link.startswith('https://t.me/'):
link = link.split('/')[-1]
else:
raise RuntimeError('cant parse link', link)
r = tg.call_method('searchPublicChat', [{'username', link}])
r.wait()
if r.error:
raise RuntimeError(r.error_info)
assert(r.update['#type'] == 'chat')
return r.update['id']

Is it possible to send a single message to multiple numbers at a time using Twilio?

I'm developing an app that allows users to add people, info, and Name/phone, or select multiple numbers from their iPhone contact list to send SMS messages to the selected numbers. the problem is Twillio API needs to be call every time per number. Is their any way to call the API once for multiple numbers?
Is it possible to send message to multiple number at a time?
Is it possible to send multiple messages?
Thanks in advance
It's not possible, you need to iterate through the list and make one request per message (which is probably better than batching it and dealing with the potential of multiple errors / resends).
Each new SMS message from Twilio must be sent with a separate REST API request. To initiate messages to a list of recipients, you must make a request for each number to which you would like to send a message. The best way to do this is to build an array of the recipients and iterate through each phone number.
const numbersToMessage = ["+15558675310", "+14158141829", "+15017122661"]
numbersToMessage.forEach(async number => {
const message = await client.messages.create({
body: 'message body',
from: '+16468635472',
to: number
});
console.log(message.status)
});
Yes this is possible. Infact i'm trying to do the same thing at the moment(which is why i'm here) and Twilio has some advanced stuff that lets us achieve this.
Assuming you have a twilio ssid, twilio auth token and a twilio phone number, the next thing you have to do is create a "Twilio Messaging Service" from the dashboard. You can use the ssid of the created messaging service and use or if you want to send a message to like 10k numbers in one go, you create a "Twilio Notify Service" from the dashboard which takes the previously created messaging service as part of its configuration. Once this is done you can call the twilio.notifications.create() and pass bindings({ binding_type: 'sms', address: number }) for each phone number to it.
Complete explanation found in this twilio blog right here with perfectly working code.
https://www.twilio.com/blog/2017/12/send-bulk-sms-twilio-node-js.html
Yes it is possible to send message to multiple user's from your Twilio Number.
You can try this for your node.js file:
var arr = ["+1xxxxxxxxxx","+1xxxxxxxxx"];
arr.forEach(function(value){console.log(value);
client.messages.create({
to:value,
from: "+19253504188",
body: msg,
}, function(err,message){
console.log(err);
});
});
Yes it is possible. You have to provide the numbers as a list and iterate API call.
For example send a message to two numbers.
numbers = ['+1234562525','+1552645232']
for number in numbers:
proxy_client = TwilioHttpClient()
proxy_client.session.proxies = {'https': os.environ['https_proxy']}
client = Client(account_sid, auth_token, http_client=proxy_client)
message = client.messages \
.create(
body="Your message",
from_='Your Twilio number',
to=number
)

How to get private messages related to a particular user programatically?

I want to get private messages (provided by the Privatemsg module) that are related to a particular user, programatically.
How can I do this?
Private messages are entities so you can use entity_load() to load them by condition...or use the wrapper provided by the private message module itself:
$messages = privatemsg_message_load_multiple(array(), array('author' => $uid));
That will get all messages created by the user identified by $uid.