def echo(update, context): text = update.message.text data = text.replace("/echo ", "") try: BOT.send(data) except Exception as e: logger.info(f"{e}")
def setName(update, context): text = update.message.text name = text.replace("/rename ", "") BOT.setName(name) with open('resource/name.txt', 'w') as file: file.write(str(name)) update.message.reply_text(f"Set bot name '{BOT.name()}'")
def unlinkChannel(update, context): file = 'resource/channel_id.txt' resultMessage = f"{BOT.name()} unlinked the channel" if os.path.isfile(file): os.remove(file) BOT.setChannelID(None) logger.info(resultMessage) update.message.reply_text(resultMessage) else: update.message.reply_text('No link')
def broadcast(update, context): text = update.message.text index = text.find(" ") data = text[index:] try: BOT.broadcast(data) update.message.reply_text(f"Broadcast is successful") except Exception as e: logger.info(f"{e}") try: BOT.send(f"Broadcast failed. Channel unlinked") except Exception as e: logger.info(f"{e}")
def linkChannel(update, context): text = update.message.text link = text.split()[-1] try: data = BOT.sendTo(f'@{link}', INTRO_MESSAGE) channel_id = data['chat']['id'] if channel_id: BOT.setChannelID(channel_id) with open('resource/channel_id.txt', 'w') as file: file.write(str(channel_id)) update.message.reply_text(f"{BOT.name()} is linked 't.me/{link}'") except Exception as e: errorMessage = f"CANNOT link 't.me/{link}'. ({e})\n" \ f"1. Is the channel PUBLIC?\n" \ f"2. Is the channel NAME CORRECT?\n" \ f"3. Is {BOT.name()} a MEMBER of the channel?" logger.info(errorMessage) update.message.reply_text(errorMessage)
import sys from Util import * # 자원 폴더 확인 if not os.path.exists('resource'): os.mkdir('resource') # token 획득, 없으면 종료 TOKEN = load('resource/token.txt') if (not TOKEN) or (TOKEN == ''): logger.info('No Token. Program exit.') sys.exit(0) else: # token 유효성 확인 try: BOT.setToken(TOKEN) except Exception as e: logger.info(f'Error: {e}') sys.exit(0) # name 획득, 없으면 기본 이름 NAME = load('resource/name.txt') if (not NAME) or (NAME == ''): logger.info('No name. Set default name') BOT.setName('준식') else: BOT.setName(NAME) # user_chat_id 획득, 없으면 메세지를 받아 획득 CHAT_ID = load('resource/user_chat_id.txt') if CHAT_ID:
from urllib.parse import urlencode from ChatBot import BOT if __name__ == '__main__': URL_for_autorize = 'https://oauth.vk.com/authorize' ID = 7656145 param = { 'client_id': ID, 'display': 'popup', 'scope': 'photos, status', 'response_type': 'token', 'v': 5.89} url = '?'.join((URL_for_autorize, urlencode(param))) print(url) a = BOT() a.chat()