我不熟悉网络技术,并且想知道是否有办法 - 使用setWebhook的想法 - 让telegram bot做简单的事情(比如简单地重复一下)每当有人向其发送消息时,一遍又一遍地发送相同的消息)没有设置服务器。
我认为可能无法绕过它,因为我需要解析JSON对象以使chat_id能够发送消息......但我希望有人在这里可能知道一种方式。
e.g。
https://api.telegram.org/bot<token>/setWebHook?url=https://api.telegram.org/bot<token>/sendMessage?text=Hello%26chat_id=<somehow get the chat_id>
我已经使用硬编码的聊天ID对其进行了测试,但它确实有效......但当然,它始终只会向同一个聊天发送消息,无论它在哪里收到消息。
答案 0 :(得分:4)
这是一个非常简单的Python bot示例,您可以在PC上运行它,无需服务器。
import requests
import json
from time import sleep
# This will mark the last update we've checked
last_update = 0
# Here, insert the token BotFather gave you for your bot.
token = 'YOUR_TOKEN_HERE'
# This is the url for communicating with your bot
url = 'https://api.telegram.org/bot%s/' % token
# We want to keep checking for updates. So this must be a never ending loop
while True:
# My chat is up and running, I need to maintain it! Get me all chat updates
get_updates = json.loads(requests.get(url + 'getUpdates').content)
# Ok, I've got 'em. Let's iterate through each one
for update in get_updates['result']:
# First make sure I haven't read this update yet
if last_update < update['update_id']:
last_update = update['update_id']
# I've got a new update. Let's see what it is.
if 'message' in update:
# It's a message! Let's send it back :D
requests.get(url + 'sendMessage', params=dict(chat_id=update['message']['chat']['id'], text=update['message']['text']))
# Let's wait a few seconds for new updates
sleep(3)
答案 1 :(得分:1)
这真的很有趣但是你肯定需要一台服务器来解析JSON值并从中获取chat_id。