

Telegram Client 开发
source link: https://arminli.com/telegram-client/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Telegram Client 开发
February 15, 2018
Telegram 有丰富的机器人 API 供开发者使用,与此同时也允许对个人账号进行 API 的调用。比如在一个群组中我想对一些内容做出自动回复,但是由于我不是管理员不能添加机器人做这个事情,只能用我自己的账号来完成,这就涉及到了其 Client 开发,同理甚至可以开发出一个全新的第三方客户端。
客户端开发有很多库,比如基于命令行的 tg-cli 和基于 Python 的 Telethon,但是我看 cli 版本好多年没更新了就没有用,这里以 Telethon 为例。
首先去 https://my.telegram.org 上申请个人开发者的标识:主要包括 apiid 和 apihash。
from telethon import TelegramClient
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('session_name', api_id, api_hash, proxy=(socks.SOCKS5, 'localhost', 8889), update_workers=4)
client.start()
第一次运行会验证账号,按照提示输入手机号(+861xxxxxxxxxx)后会在客户端上收到一串验证码,继续按照提示输入即可验证完成。
实现自动回复的全部代码:
from telethon import TelegramClient
import socks
from telethon.tl.types import UpdateNewChannelMessage
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('session_name', api_id, api_hash, proxy=(socks.SOCKS5, 'localhost', 8889), update_workers=4)
client.start()
def replier(update):
if isinstance(update, UpdateNewChannelMessage) and update.message.reply_to_msg_id is None:
client.send_message(update.message.to_id, 'Test', reply_to=update.message.id)
client.add_update_handler(replier)
input('Press enter to stop this!')
client.disconnect()
Recommend
-
191
Telegram for macOS
-
267
Telegram messenger for Android Telegram is a messaging app with a focus on speed and security. It’s superfast, simple and free. This repo contains the official source code for
-
190
matterbridge
-
189
Telebot "I never knew creating Telegram bots could be so sexy!" go get -u gopkg.in/telebot.v3 Overview Telebot is a bot framework for
-
301
-
22
目录 Vue自动化工具:Vue-Client 组件化开发 1.什么是组件? 1.组件(Component)是 自定义封装的功能 。在前端开发过程中,经常出现多个网页的功能是重复...
-
59
Armin's BlogTelegram Bot 开发的代理设置February 13, 2018在开发 Telegram Bot 时,我们要在本地测试机器人,但是其服务被墙,这里以
-
13
or 1McafEgMvqAVujNLtcJumZHxp2UfaNByqs Telegram-FOSS Telegram is a messaging app with a focus on speed and security. It’s superfast, simple and free. This is an unoffici...
-
4
使用Cobra与Client-go实现Kubernetes自定义插件开发 ...
-
11
GitHub: https://github.com/hellodk34/weibo_hot_search weibo_hot_search 微博热搜实时推送,推送到 telegram 频道。频道链接: https...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK