telegram - 阅读 Telegram channel 消息

标签 telegram

所以我需要阅读我所在的一个特定 channel 的所有新消息(而不是作为管理员)。我搜索了不同的客户端 api(.NET、PHP、nodejs),但没有一个有帮助。

你知道我怎么能做到这一点吗?

谢谢!

最佳答案

这是我如何做到的:

安装 Telegram https://github.com/vysheng/tg

安装 Cli 包装器 https://github.com/luckydonald/pytg

from pytg import Telegram
from pytg.utils import coroutine
tg      = Telegram(  telegram="./tg/bin/telegram-cli", pubkey_file="./tg/tg-server.pub")
receiver    = tg.receiver
QUIT = False
@coroutine
def main_loop():
  try:
    while not QUIT:
      msg = (yield) # it waits until it got a message, stored now in msg.
      if msg.text is None:
        continue
      print(msg.event)
      print(msg.text)
  except GeneratorExit:
    pass
  except KeyboardInterrupt:
    pass
  else:
    pass

receiver.start()
receiver.message(main_loop())   

NodeJS 版本:
const path = require('path');
const TelegramAPI = require('tg-cli-node');
const config = {
    telegram_cli_path: path.join(__dirname, 'tg/bin/telegram-cli'), //path to tg-cli (see https://github.com/vysheng/tg)
    telegram_cli_socket_path: path.join(__dirname, 'socket'), // path for socket file
    server_publickey_path: path.join(__dirname, 'tg/tg-server.pub'), // path to server key (traditionally, in %tg_cli_path%/tg-server.pub)
}

const Client = new TelegramAPI(config)

Client.connect(connection => {
    connection.on('message', message => {
        console.log('message : ', message)
        console.log('message event : ', message.event)
        console.log('message text : ', message.text)        
        console.log('message from :', message.from)
    })
    connection.on('error', e => {
        console.log('Error from Telegram API:', e)
    })
    connection.on('disconnect', () => {
        console.log('Disconnected from Telegram API')
    })
})

关于telegram - 阅读 Telegram channel 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40193172/

相关文章:

telegram 私有(private) channel 独特的邀请链接

python - 将字典循环Python转换为Json

javascript - Node.js Telegram 机器人无法将用户从 super 组中踢出

Go Telegram Bot API 从本地文件上传照片

c# - 使用 TLSharp 库转发消息 (C#)

python - 通过 telethon 查找 session ID 并终止 session

python - Telegram 对话机器人中后退按钮实现的最佳实践

java - Android错误无法比较的类型: Object and int

python - 有没有办法知道使用 telethon 的 telegram post 照片的 url?

使用 sendMediaGroup 进行 Telegram 限制