Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2417|回复: 1
打印 上一主题 下一主题

working with Telegram’s API

[复制链接]

1272

主题

2067

帖子

7958

积分

认证用户组

Rank: 5Rank: 5

积分
7958
跳转到指定楼层
楼主
发表于 2020-2-27 11:33:36 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式
Signing In¶Before working with Telegram’s API, you need to get your own API ID and hash:
  • Login to your Telegram account with the phone number of the developer account to use.
  • Click under API Development tools.
  • A Create new application window will appear. Fill in your application details. There is no need to enter any URL, and only the first two fields (App title and Short name) can currently be changed later.
  • Click on Create application at the end. Remember that your API hash is secret and Telegram won’t let you revoke it. Don’t post it anywhere!
Note
This API ID and hash is the one used by your application, not your phone number. You can use this API ID and hash with any phone number or even for bot accounts.

Editing the Code¶This is a little introduction for those new to Python programming in general.
We will write our code inside hello.py, so you can use any text editor that you like. To run the code, use python3 hello.py from the terminal.
Important
Don’t call your script telethon.py! Python will try to import the client from there and it will fail with an error such as “ImportError: cannot import name ‘TelegramClient’ …”.


Signing In¶We can finally write some code to log into our account!
from telethon import TelegramClient# Use your own values from my.telegram.orgapi_id = 12345api_hash = '0123456789abcdef0123456789abcdef'# The first parameter is the .session file name (absolute paths allowed)with TelegramClient('anon', api_id, api_hash) as client:    client.loop.run_until_complete(client.send_message('me', 'Hello, myself!'))

In the first line, we import the class name so we can create an instance of the client. Then, we define variables to store our API ID and hash conveniently.
At last, we create a new TelegramClient instance and call it client. We can now use the client variable for anything that we want, such as sending a message to ourselves.
Note
Since Telethon is an asynchronous library, you need to await coroutine functions to have them run (or otherwise, run the loop until they are complete). In this tiny example, we don’t bother making an async def main().
See Mastering asyncio to find out more.

Using a with block is the preferred way to use the library. It will automatically start() the client, logging or signing up if necessary.
If the .session file already existed, it will not login again, so be aware of this if you move or rename the file!

Signing In as a Bot Account¶You can also use Telethon for your bots (normal bot accounts, not users). You will still need an API ID and hash, but the process is very similar:
from telethon.sync import TelegramClientapi_id = 12345api_hash = '0123456789abcdef0123456789abcdef'bot_token = '12345:0123456789abcdef0123456789abcdef# We have to manually call "start" if we want an explicit bot tokenbot = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)# But then we can use the client instance as usualwith bot:    ...

To get a bot account, you need to talk with @BotFather.

Signing In behind a Proxy¶If you need to use a proxy to access Telegram, you will need to  install PySocks and then change:
TelegramClient('anon', api_id, api_hash)

with
TelegramClient('anon', api_id, api_hash, proxy=(socks.SOCKS5, '127.0.0.1', 4444))

(of course, replacing the IP and port with the IP and port of the proxy).
The proxy= argument should be a tuple, a list or a dict, consisting of parameters described in PySocks usage.

Using MTProto Proxies¶MTProto Proxies are Telegram’s alternative to normal proxies, and work a bit differently. The following protocols are available:
  • ConnectionTcpMTProxyAbridged
  • ConnectionTcpMTProxyIntermediate
  • ConnectionTcpMTProxyRandomizedIntermediate (preferred)
For now, you need to manually specify these special connection modes if you want to use a MTProto Proxy. Your code would look like this:
from telethon import TelegramClient, connection#   we need to change the connection ^^^^^^^^^^client = TelegramClient(    'anon',    api_id,    api_hash,    # Use one of the available connection modes.    # Normally, this one works with most proxies.    connection=connection.ConnectionTcpMTProxyRandomizedIntermediate,    # Then, pass the proxy details as a tuple:    #     (host name, port, proxy secret)    #    # If the proxy has no secret, the secret must be:    #     '00000000000000000000000000000000'    proxy=('mtproxy.example.com', 2002, 'secret'))

In future updates, we may make it easier to use MTProto Proxies (such as avoiding the need to manually pass connection=).
In short, the same code above but without comments to make it clearer:
from telethon import TelegramClient, connectionclient = TelegramClient(    'anon', api_id, api_hash,    connection=connection.ConnectionTcpMTProxyRandomizedIntermediate,    proxy=('mtproxy.example.com', 2002, 'secret'))


回复

使用道具 举报

1272

主题

2067

帖子

7958

积分

认证用户组

Rank: 5Rank: 5

积分
7958
沙发
 楼主| 发表于 2020-2-27 11:51:35 | 只看该作者

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-11-23 03:32 , Processed in 0.065034 second(s), 22 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表