Overhear telegram chat with your customer
Somehow I wanted to, the messages of one of the telegraph chat rooms were stored on my disk (without starting a regular client). I will not reveal their motives, but the possibility of this seemed necessary and useful.
3r33450.
To this end, in a telegram has the bots. Habré has several articles dealing with bots, for example: ". Chat Assistant to the site ".
3r33450.
The bot allows you to read and send messages, you do not need a phone to register a bot and there can be any number of bots. But the bot name includes the word "bot", which can cause a host chat unnecessary questions.
3r33450.
But as they say, right question - half of the answer.
https://telegram.org/apps#source-code
3r33450.
However, the easiest to use was the python library: Pure Python 3 MTProto API Telegram client library called
"telethon"
3r33450.
Only here is the problem. I do not know python. Well, there is a reason to meet.
According to a manual on telethon, his installation is very simple. Enough to run the command line:
pip3 install telethon
Pitfalls, met me at installation:
PIP3 is not installed (installer for Python).
sudo apt-get -y install python3-pip
3r376. The library works only on python version> 3.5. So, you may have to update it.
3r33450.
Everything is established. Flipping readme.txt on.
The next item is the creation of a telegraph client How, already? Well, it's simple. However, you must first zaregistritrovat itself as the creator of the client.
3r33450.
Go to the website telegram: https://my.telegram.org
3r33450. We introduce the phone and wait for the confirmation code in the native client telegram. He is quite long (12 characters) and inconvenient to enter.
Go to the item " API ". We look for the "Telegram API" and go to the "Creating an application" ( [url]Https://my.telegram.org/apps[/url] ).
Fill field
App title
and
Short name
, Press
«Create application»
and remember two variables:
api_id
and 3r3138. api_hash
.
It's time to make the customer.
3r33450. from telethon import TelegramClient, sync
3r33450. # Insert api_id and api_hash
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
3r33450. client = TelegramClient ('session_name', api_id, api_hash)
client.start ()
3r33450.
session_name - you can insert any name. You will be asked to enter the phone number and send a confirmation code. The client will work without a telephone request (as long as not to change session_name). The file will appear next to your program. session_name.session
If there are no errors, the client is ready. Only here, no output. Let's try to get some useful inforfmatsiyu.
3r33450.
We learn a little about yourself:
print (client.get_me (). stringify ())
the result is given as:
User (
photo = None,
last_name = 'Pupkin',
first_name = 'Vasya',
id = 12345678?
phone = '79041234567',
- Something else.
)
We can send a message from yourself:
client.send_message ( 'username', 'Hello! Talking to you from Telethon')
3r33450.
You can and picture 3r3444.
client.send_file ( 'username', '/home/myself/Pictures/holidays.jpg')
3r33450.
How do others:
client.download_profile_photo ( 'me')
We look at what we signed chats:
print all chats name
for dialog in client.iter_dialogs ():
print (dialog.title)
3r33450.
read all chat messages "chat_name" (carefully, there can be a lot of messages)
messages = client.get_entity ( 'chat_name')
print (messages)
3r33450.
view all users
chat. participants = client.get_participants ( 'chat_name')
print (participants)
Indulged?
3r33450. Now, in fact, we are doing something for which we all started this
We need a program that is watching the new messages in a specific channel.
3r33450.
That the client did not finish work, after client.start () we interpose a line:
3r33450.
client.run_until_disconnected (
).
3r33450.
This construct (inserted before client.start ()) displays only new messages:
3r33450.
@ Client.on (events.NewMessage (chats = ( 'chat_name')))
async def normal_handler (event):
# Print (event.message)
print (event.message.to_dict ()['message'])
Let's face it.
3r33450.
@ Client.on (events.NewMessage (chats = ( 'chat_name')))
3r33450.
Creates an event that fires when a new message appears
3r33450.
print (event.message)
3r33450.
displays a message like this:
3r33450.
Message (edit_date = None, views = None, reply_markup = None, fwd_from = None, id = 18? entities =[], Post = False, mentioned = False, via_bot_id = None, media_unread = False, out = True, media = None , date = datetime.datetime (201? 1? ? ? 2? 2? tzinfo = datetime.timezone.utc), to_id = PeerChannel (channel_id = 123456789), reply_to_msg_id = None, from_id = 12345678? silent = False, grouped_id = None, post_author = None, message = 'hello telegram')
From all this we need a field: "message = 'hello telegram'":
3r33450.
print (event.message.to_dict ()['message'])
3r33450.
The message received, but from whom it is not clear, because in a message only a user ID. To match the user ID and the name, download all IM users and put them in a dictionary (hash) as d[id]= "first_name last_name"
participants = client.get_participants (group)
users = {}
3r33450. for partic in client.iter_participants (group):
lastname = ""
if partic.last_name:
lastname = partic.last_name
users[partic.id]= partic.first_name + "" + lastname
Now we can find out who sent the message:
3r33450.
s_user_id = event.message.to_dict ()['from_id']
user_id = int (s_user_id)
user = d.get (user_id)
3r33450.
In principle, you can get the user name from the telegram directly, but if people are a little bit easier with the dictionary.
Pull out from a message sent date:
3r33450.
mess_date = event.message.to_dict (['date']). 3r33434.
3r33450.
All, all of the data we have. It remains to be written to a file.
3r33450. To do this, first open the file for writing:
f = open ('messages_from_chat', 'a')
And write the message:
f.write (mess_date.strftime ( "% d-% m-% Y% H:% M") + "n")
f.write (user + "n")
f.write (user_mess + "nn")
f.flush (
).
3r33450.
That's all! All that I needed, the program does. Utility, of course, buggy, but its task is performed.
Python was not so difficult 3r33384. as he is painted , Especially description and the different libraries on the Internet is full. Write a couple more utilitok and getting used to it, you can use it as a scripting language instead of bash.
3r33450. 3r33333. 3r33333. All of the text utility: [/b]
from telethon import TelegramClient, sync, events
).
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
3r33450. client = TelegramClient ('session_name', api_id, api_hash)
3r33450. @ client.on (events.NewMessage (chats = ('chat_name')))
async def normal_handler (event):
# Print (event.message)
user_mess = event.message.to_dict (['message']). 3r33450. 3r33450. s_user_id = event.message.to_dict ()['from_id']3r33450. user_id = int (s_user_id)
user = d.get (user_id)
3r33450. mess_date = event.message.to_dict (['date']). 3r33450.
f.write (mess_date.strftime ( "% d-% m-% Y% H:% M") + "n")
f.write (user + "n")
f.write (user_mess + "nn")
3r33450. f.flush ()
3r33450. client.start ()
group = 'group_name'
participants = client.get_participants (group)
users = {}
for partic in client.iter_participants (group):
lastname = ""
if partic.last_name:
lastname = partic.last_name
users[partic.id]= Partic.first_name + "" + lastname
f = open ('messages_from_chat', 'a')
3r33450. client.run_until_disconnected ()
f.close (
Full description of the library. .
3r33450.
It may be interesting
weber
Author3-10-2018, 00:11
Publication DateDevelopment / Programming
Category- Comments: 0
- Views: 411
Miro Paris vous propose une large gamme d'accessoire de beauté, maquillage, skincare, kbeauty, crèmes en provenance de Corée du sud, Japon, et Hong Kong. Check Out: Cosmétiques Asiatiques